Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
This program should show The total is 50. It stops with an error. Find the fault and fix it.
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.
Plan. Follow the program. The function add uses print, and it has no return. So it shows 50, and gives back None.
Then total is None, and total + 0 is None + 0, which gives TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'.
The fix. Make the function give its answer back.
Check. add(20, 30) is now 50, and the program shows The total is 50.
Answer: Replace print(a + b) with return a + b.
Log in to join the discussion.
Loading…