我知道这是不正确的,因为“ input()”只能接受1个参数,但是我想在要求用户输入更多数字的同时在输入中包含这些变量:
n = 5
average = 0
for i in range(n):
numbers = eval(input('Please enter number ', (i+1) ,' of ', n ,' to average:',sep='',end=''))
average = average+numbers/n
最佳答案
您可以使用字符串格式和F字符串(仅适用于Python 3.6+)
字符串格式:
n = 5
average = 0
for i in range(n):
numbers = int(input('Please enter number {} of {} to average:'.format(i+1, n)))
average = average+numbers/n
F字符串(适用于Python 3.6及更高版本):
n = 5
average = 0
for i in range(n):
numbers = int(input(f'Please enter number {i+1} of {n} to average:'))
average = average+numbers/n
关于python - 如何在输入函数中引用变量?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52807268/