简体   繁体   English

如何在while循环中添加所有用户输入?

[英]How to add all the user inputs in a while loop?

x=input("Do you want to enter number Y/N:")
numbersEntered= 0
while x == "Y":
    number = int(input("Enter number :"))
    numbersEntered=numbersEntered+1
    y=input("Do you want to continue entering new numbers: Y/N:")
    if y == "N":
        break
average=/numbersEntered

print(average)

Using Python.This is what I have so far. 使用Python,这是我到目前为止所拥有的。 I need to add all the user inputs together once the user enters "N". 用户输入“ N”后,我需要将所有用户输入加在一起。 I just do not know how to do this. 我只是不知道该怎么做。 Any help is appreciated. 任何帮助表示赞赏。

You don't 'need to add all the user inputs together once the user enters "N"', per se . 就其本身而言 ,一旦用户输入“ N”,您就不需要“将所有用户输入加在一起”。

Create a new variable, total (don't call it sum , that's a Python built-in), initialise it to 0 and add number to it with each iteration. 创建一个新变量total (不要将其称为sum ,这是Python的内置函数),将其初始化为0并在每次迭代中添加number You'd do this in much the same way as you're currently calculating numbersEntered . 您将以与当前计算numbersEntered相同的方式执行此numbersEntered

Then you need to correct your average calculation to take total into account. 然后,您需要更正average计算以将total考虑在内。

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

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