简体   繁体   中英

Adding up user input in For Loop, Break after 0 input

I need the user to key in as many integer inputs. I'm aware that I have to use a for loop. 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.

Example: there's 100 stalls. The program will keep prompting which stalls the user visited until 0 is entered. User entered 1, 3, 6, 10, 20, 0 . How do I program to add 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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