Learn Before
Concept

Common Semantic Errors

  • String concatenation instead of arithmetic addition
num1 = input('Enter a number:') num2 = input('Enter another number:') sum = num1+num2 print('The sum of', num1, 'and', num2, 'is', sum)

This program runs and produces a result, but it is not the result intended. This is because the program contains a semantic (logic) error. The error is that the program performs string concatenation instead of integer addition. To solve this, the program must perform the necessary integer conversions before calculating the sum.

Fix:

sum = int(num1)+int(num2)
  • Order of operations
x = int(input('Enter a number')) y = float(input('Enter a number')) z = x+y/2 print('Average:', z)

This program runs and produces a result, but it is not the result intended. This is because the program contains an error in arithmetic order of operations.

Fix:

z = (x+y)/2

0

1

Updated 2021-06-15

Contributors are:

Who are from:

Tags

Python Programming Language

Data Science