[英]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.