+ 1

What’s the difference? - Do The Math

sum = 0 while True: x = input() sum += int(x) if x == "stop": break print(sum) & sum = 0 while True: x = input() if x == "stop": break sum += int(x) print(sum) I wonder what’s the difference between these two coding? The first doesn’t work but second does, they seem similar to me!

4th Mar 2021, 7:30 AM
Aaron Siaw
Aaron Siaw - avatar
2 ответов
+ 8
The first will give you an error if the user enters stop since you're attempting to convert 'stop' to an int. The second will instead check if the user has entered stop and will exit the loop prior to attempting the conversion. It will still error though if a value other that 'stop' or an integer is entered by the user.
4th Mar 2021, 7:43 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
I see! Got it! Thanks! ChaoticDawg
4th Mar 2021, 7:54 AM
Aaron Siaw
Aaron Siaw - avatar