简体   繁体   English

Python-如何在循环中多次使用扫描仪?

[英]Python - How do I use a scanner multiple times in a loop?

I am trying to learn Python and I was trying to test what I just learned with a simple program. 我正在尝试学习Python,并且试图通过一个简单的程序测试刚刚学到的东西。 The program asks the user how many numbers they want to enter and then the program will use a loop to store the amount of numbers and find the average. 该程序询问用户要输入多少个数字,然后该程序将使用循环来存储数字量并找到平均值。

numberOfTimes = int(input("How many numbers do you want to use today? "))

count = 0

while(count < numberOfTimes):
    numberInput = int(input("Enter the number: "))
    count += 1

print((numberInput + numberInput + numberInput) / (count))

How do I get numberInput to store each variable? 如何获取numberInput存储每个变量? Thanks for the help in advanced! 感谢您对高级的帮助!

您可以在循环的上方声明一个类似于totalNumber的变量,然后在循环中要做的就是以下操作:

totalNumber += int(input("Enter your number: ")

You don't need to store all the numbers, just sum them as you go: 您不需要存储所有数字,只需在进行操作时将它们相加即可:

count = 0
total = 0

while(count < numberOfTimes):
    total += int(input("Enter the number: "))
    count += 1

print(total / count)

Congrats for learning python, it's a great language. 祝贺您学习python,这是一门很棒的语言。

So if you want to have the biggest flexibility you should save your numbers in a list. 因此,如果您想拥有最大的灵活性,则应将数字保存在列表中。

A list in python behaves like a classically known array list (eg in java) because it can be accessed by index and it can be extended and has no fixed length. python中的列表的行为类似于经典已知的数组列表(例如,java中的列表),因为它可以通过索引访问,并且可以扩展并且没有固定的长度。

So using a list you can add every number into a list: 因此,您可以使用列表将每个数字添加到列表中:

numbers = list()

while(count < numberOfTimes):
    numberInput = int(input("Enter the number: "))
    numbers.append(numberInput)
    count += 1

Since you are now using a list of variable length you could also use the lengths ( len(mylist) list to determine the end of the while loop: 由于您现在使用的是可变长度的列表,因此也可以使用长度( len(mylist)列表来确定while循环的结尾:

numbers = list()

while(len(numbers) < numberOfTimes):
    numberInput = int(input("Enter the number: "))
    numbers.append(numberInput)

Combining all these steps: 结合所有这些步骤:

numberOfTimes = int(input("How many numbers do you want to use today? "))
numbers = list()

while(len(numbers) < numberOfTimes):
    numberInput = int(input("Enter the number: "))
    numbers.append(numberInput)

print( sum(numbers) / len(numbers) )

I added a little functionality to make it easier eg sum(numbers) is a build in function than sums up all the integers in the list and len(numbers) again replacing the counter. 我添加了一些功能使其变得更容易,例如sum(numbers)是一个内置函数,而不是对列表中的所有整数求和,然后再次用len(numbers)代替计数器。

Executing the program like that will show you quite quick, that you only get integer numbers as result which are not the correct average, here a you need to know that dividing two integers in python results in an integer which is most of the time not the result you want. 执行这样的程序将很快显示出结果,您只会得到整数值,该整数值不是正确的平均值,这里您需要知道在python中将两个整数相除会得到一个整数,该整数在大多数情况下不是你想要的结果。 There is a trick, cast one of the numbers (or both) to be floats, then divide: 有一个技巧,将其中一个(或两个)转换为浮点数,然后除以:

average = float(sum(numbers)) / len(numbers)
print(average)

If you want to try some more steps you could eg get rid of the first question to ask for an amount of numbers by jumping out of the while loop depending on the content you type in: 如果您想尝试更多的步骤,可以例如根据输入的内容,通过跳出while循环来摆脱第一个询问数量的数字的问题:

print("Write some numbers, I will calculate the average. Finish with 'enter'")
numbers = list()

while(True):
    myinput = raw_input("Enter a number: ")
    if myinput == '':
        break
    numberInput = int(myinput)
    numbers.append(numberInput)

average = float(sum(numbers)) / len(numbers)
print(average)

The changes are this time: use raw_input instead of input which would be recommended, the while loop runs for ever if it doesn't get to the break which is executed if you don't give any input but just hit enter and the initial question for the count obviously is missing. 这次是这次更改:使用raw_input而不是推荐的input ,如果没有到达break ,则while循环将永远运行,如果您不提供任何输入而只是按回车键和初始问题,则会执行while循环因为显然没有。

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

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