0
Why doesn’t this code work?
num1 = input() num2 = input() print(int(num1)) print(int(num2)) print(int(num1)+=int(num2)) It is saying there is a syntax error... Can anyone tell me why?
4 ответов
+ 3
🌌DarkFox
num1 = int(input())
num2 = int(input())
print(num1)
print(num2)
print((num1)+num2)
just declare int once per var input and yes get rid of the equal sign
0
because you put an equal sign after a plus sign.
delete the equal (=) sign after the plus (+) sign and your code will work.
0
depending on what you're trying to achieve, you could also do:
num1 += num2
print(num1)
+= is kind of shortcut for:
num1 = num1 + num2
this will works without error as soon as num1 and num2 values are of same type...
however, doing:
print(num1 += num2)
will not display expected result but None instead, as in Python assignement doesn't return anything (None) ^^
0
obviously if you want to add two numbers but you get it through input (string), you must convert them before to numbers:
num1 = int(num1)
num2 = int(num2)
else you will concatenate the two string rather than add the two numbers ;)