Why doesn’t this code work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

24th Mar 2021, 11:04 PM
🌌DarkFox
🌌DarkFox - avatar
4 Answers
+ 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
25th Mar 2021, 3:53 AM
BroFar
BroFar - avatar
0
because you put an equal sign after a plus sign. delete the equal (=) sign after the plus (+) sign and your code will work.
24th Mar 2021, 11:09 PM
Dino Wun (Use the search bar plz!)
Dino Wun (Use the search bar plz!) - avatar
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) ^^
25th Mar 2021, 3:57 AM
visph
visph - avatar
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 ;)
25th Mar 2021, 4:00 AM
visph
visph - avatar