Python calculator question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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
2 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
+ 2
Thank you JUMP_LINK__&&__Python__&&__JUMP_LINK Learner Python Learner and ♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ Kiibo Ghayal! I appreciate both your input!
5th Sep 2021, 2:23 AM
Ben Asplund