简体   繁体   中英

How can I sum user input numbers whilst in a loop?

I'm trying to get the sum of numbers that a user inputs in a loop, but I can't get it to include the first number input - here's what I have so far

number = int(input("Enter a number"))
total = 0
while number != -1:
 number = int(input("Enter another number"))
 total += number
else:
  print(total)

Probably something easy I'm missing but I'm stumped ( i am a beginner as you can tell)

I have tried changing the name of the first variable number but I end up in a constant loop even when number = -1

number = int(input("Enter a number"))
total = 0
while number != -1:
    total += number
    number = int(input("Enter another number"))
else:
    print(total)

Just move the summation one line above.

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