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!
4 Respostas
+ 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)
+ 2
Thank you JUMP_LINK__&&__Python__&&__JUMP_LINK Learner Python Learner and ♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ Kiibo Ghayal! I appreciate both your input!