简体   繁体   English

如何在循环中对用户输入的数字求和?

[英]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 = -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.只需将求和向上移动一行。

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

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