Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A program reads two numbers with a = input() and b = input(), then shows a + b. The user types 3 and 4, and the output is 34. Explain why, and fix the program.
Convert text to a number with int() or float() before you calculate.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Why. input() always gives text. So a is "3" and b is "4". With text, the + sign joins, so the result is "34".
Fix. Convert each one to a number first: int(a) + int(b), which is 7.
Check. The program below shows both: Joined: 34 and Added: 7.
Log in to join the discussion.
Loading…