+ 1
Can I determinate the number of input before require them ?
I want to determine the number of inputs before require/use them. (example) ask = int (input ('How many people are in the room ?') #ask = 2 p1 = int (input ('Type age: ')) p2 = int (input ('Type age: ')) (because the number of persons are two). But if was 120, each one with a different age ? #ask = 120 p1 = int (input ('Type age: ')) p2 = int (input ('Type age: ')) ... pn = int (input ('Type age: ')) (n times) And I need use this "pn" inputs. ask = 2 res =( p1 + p2 ) / 2 ask = n res = (p1+..pn)/n
1 Réponse
+ 1
You can just ask for the persons' age in a loop and put the results in a list. So first you ask for the number of people the way you already do. That gives you the number n. Now you can do this:
persons = []
for i in range(n):
persons.append(int(input('Type age: ')))
You can then access the age of e.g. p1 via persons[0].