简体   繁体   English

你如何让 Python 向用户询问输入“x”的次数?

[英]How do you get Python to ask the user for an input 'x' amount of times?

I am trying to get Python to ask the user the same question 'x' amount of times depending on what the user determines 'x' to be.我试图让 Python 根据用户确定的“x”向用户询问相同的问题“x”次数。 So I am trying to achieve for Python to ask the user 'how many numbers would you like to find the average for'.所以我试图实现 Python 询问用户“你想找到多少个数字的平均值”。

I thought the first line could be我以为第一行可能是

x = eval(input('How many numbers would you like to find the average for?: '))

and then for Python to ask something like 'Enter a number' an x amount of times.然后对于 Python 询问诸如“输入数字”之类的内容 x 次。

Do not use eval .不要使用eval Convert the input to int将输入转换为int

x = int(input('How many numbers would you like to find the average for?: '))

Then use that to loop然后用它来循环

values = []
for i in range(x):
    values.append(int(input('Please enter a value')))
x = eval(input('How many numbers would you like to find the average for?: '))
for count in range (x):
    amount = input (f"Enter number{count+1}:\n")

This will basically help you to ask the user for repeated inputs depending on the amount they entered, and also when the program asks the user for input every time it will go as follows: Enter number 1 Enter number 2 and so on until the program is completed and you can change Enter Number to anything you like.这将基本上帮助您根据他们输入的数量要求用户重复输入,并且当程序每次要求用户输入时,它会按如下方式进行:输入数字 1输入数字 2依此类推,直到程序运行完成,您可以将输入数字更改为您喜欢的任何内容。 Copy the code and see if that's what you wanted, let me know if something doesn't work out.复制代码,看看这是否是您想要的,如果有什么不成功,请告诉我。 Good luck with the program and hopefully this helped.祝项目好运,希望这会有所帮助。 I mean I just learned this myself about the looped inputs.我的意思是我自己刚刚了解到有关循环输入的知识。

At the start you can create a empty "numbers" list:在开始时,您可以创建一个空的“数字”列表:

numbers = []

And I'd recommend for you to use int() instead of eval() .我建议您使用int()而不是eval()

x = int(input('How many numbers would you like to find the average for? '))

Then you can start a for() loop and put an input inside it:然后您可以启动一个for()循环并在其中输入一个输入:

for i in range(x):
    n = int(input('Number ' + str(i+1) + ': '))

And then still in the loop use append() to put the numbers in the list:然后仍在循环中使用append()将数字放入列表中:

for i in range(x):
    n = int(input('Number ' + str(i+1) + ': '))
    numbers.append(n)

Then for the calculation at the end you can use sum() to get the sum of all the numbers in the list, and then divide it by the length (using len() ) of the list:然后对于最后的计算,您可以使用sum()获取列表中所有数字的总和,然后将其除以列表的长度(使用len() ):

final = sum(numbers) / len(numbers)

But if you want to round the final number, use round() :但是,如果要对最终数字进行四舍五入,请使用round()

final = round(sum(numbers) / len(numbers))

And this is the final code:这是最终的代码:

numbers = []

x = int(input('How many numbers would you like to find the average for? '))

for i in range(x):
    n = int(input('Number ' + str(i+1) + ': '))
    numbers.append(n)

final = sum(numbers) / len(numbers)
finalRounded = round(final)

Then you would obviously print it out.然后你显然会打印出来。

use a forLoop statement...使用 forLoop 语句...

read x, then读取 x,然后

for y in range(0, x): user input goes here (y is an int that will show how many times youve gone into the for statement) for y in range(0, x): 用户输入到这里(y 是一个 int,将显示你进入 for 语句的次数)

and thats mainly it主要是这样

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

相关问题 Python:如何获得一个程序来提示用户一定的固定时间来输入用户信息? - Python: How do you get a programme to prompt the user a certian fixed amount of times for a user input? 您如何要求用户在Python上输入矩阵? - How do you ask a user to input a matrix on Python? 当用户为变量分配了用户选择输入的“ x”次次数时,如何创建变量? - How do I create variables as they are being assigned by the user a 'x' amount of times that the user chooses to input? 如何根据用户对第一个问题的输入提示用户 x 次 - How do I promt user x amount of times based on their input on the first question 如何根据用户输入循环x次 - How to loop x amount of times based on user input 您如何在列表理解中要求用户输入? - How do you ask for user input in list comprehension? 我如何从列表中随机选择用户要求在 python 中使用输入的次数? - how do i randomly pick characters out of a list the amount of times the user asks for using input in python? 你如何让某物在 python 中循环特定次数? - how do you make something loop a specific amount of times in python? 如何使用 python 通过用户输入在范围内打印一定数量的结果? - How do you print certain amount of results in range by user input using python? 如何在 vscode 中的 python 中获得用户输入? - How do you get a user input in python in vscode?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM