0

Python calculator question

I am trying to write a code that adds up all the numbers of a given input, and breaks when the user enters "stop". My code: sum=0 while True: x = input() if x == "stop": break else: sum = sum+int(x) print(sum) the issue I'm running in to is that when I print the sum, all the numbers inputted print in addition to the final number. Thanks in advance!

5th Sep 2021, 1:55 AM
Ben Asplund
4 Answers
+ 4
Hi Judge! That's because you're printing the output inside loop. So, it prints sum value for each iteration. Inorder to print the final sum you can make it outside loop. Here it is what I mentioned above. sum=0 while True: x = input() if x == "stop": break else: sum = sum+int(x) print(sum)
5th Sep 2021, 2:02 AM
Python Learner
Python Learner - avatar