I know this is incorrect because 'input()' can only take 1 argument, but I want to include these variables in the input while asking the user to enter more numbers:
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
2 Answers 2
You can use string formatting and F-Strings ( only Python 3.6+ )
String Formatting:
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-Strings (Works in 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
answered Oct 14, 2018 at 21:35
DarkSuniuM
2,5822 gold badges31 silver badges45 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You could try concatenating your variables after converting them to string [str(x)]
n = 5
average = 0
for i in range(n):
numbers = eval(input('Please enter number '+str(i+1)+' of '+str(n)+' to average:'))
average = average+numbers/n
This is a naive approach but works fine.
answered Oct 14, 2018 at 21:53
user7713248
Comments
lang-py
inputisn't likeprint, it takes a single string to show as the prompt. You have to create that string yourself - Python has several options for string formatting you could do some research on. Also you should almost never useeval.