简体   繁体   English

在 For 循环中添加用户输入,在 0 输入后中断

[英]Adding up user input in For Loop, Break after 0 input

I need the user to key in as many integer inputs.我需要用户输入尽可能多的 integer 输入。 I'm aware that I have to use a for loop.我知道我必须使用 for 循环。 My issue is, how do I add up all the integers the user key in together?我的问题是,如何将用户键入的所有整数加在一起? The for loop will only break when user key in 0.只有当用户键入 0 时,for 循环才会中断。

Example: there's 100 stalls.示例:有 100 个摊位。 The program will keep prompting which stalls the user visited until 0 is entered.程序会一直提示用户访问了哪些档位,直到输入 0。 User entered 1, 3, 6, 10, 20, 0 .用户输入1, 3, 6, 10, 20, 0 How do I program to add 1 + 3 + 6 + 10 + 20 = 40 ?如何编程添加1 + 3 + 6 + 10 + 20 = 40

for i in range(101):
   stalls = int(input("Enter stall number to visit: "))
sum = 0
for i in range(101):
   stalls = int(input("Enter stall number to visit: "))
   sum+=stalls
   if stalls == 0:
       break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM