Request n entries in python

2

I am having difficulty in this exercise:

"Make a program that asks n people their age, at the end the program should check if the average age of the class varies between 0 and 25, 26 and 60 and greater than 60, and then say if the class is "young," "adult," or "old," according to the calculated mean, the program should end when -1 is typed. "

I do not know how to ask the age of n people. How would I do that?

    
asked by anonymous 11.11.2017 / 23:55

3 answers

3
  

I do not know how to ask the age of n people. How would I do that?

idade = 0
lista_com_idades = []
while idade != '-1':
    idade = input('Diga sua idade: ')
    lista_com_idades.append(idade)

This is the answer to your question. As long as the user does not type '-1' he keeps asking age. I hope I have helped!

    
12.11.2017 / 00:54
2

In Python 3. I do not know if I understand the doubt. But the following works:

z=0
x=[]
while z != -1:
    z = int(input('Digite a idade: '))

    if z != -1:
        x.append(z)

if len(x) != 0:
    media = sum(x)/len(x)

m = round(media,0)

if 0 <= m <= 25:
    print('Populacao jovem')

if 26 <= m <= 60:
    print('Populacao adulta')

if m > 60:
    print('Populacao idosa')

Would that be?

EDIT: error in the round, should be 0, not 1. Tbm includes m "if" to check if you had any age typed.

    
12.11.2017 / 00:35
-1

You can loop through a range of data by the user:

n = raw_input("Quantas pessoas são?")
for i in range(1,n):
blabla
    
12.11.2017 / 00:11