0
I’m trying to create a script that reads if the person is older or younger. I would like if for example only 1 person is bigger, it writes '1 person' and not '1 people', but my code is giving some error if all people are smaller or larger, but it works if I merge. What can it be?
from datetime import date
atual = date.today().year
maior = menor = cont = 0
for nasc in range(1, 4):
cont += 1
nasc = int(input(f'Ano de nascimento da {cont}ª pessoa:'))
idade = atual - nasc
if idade >= 18:
maior += 1
if maior != 1:
p1 = 'pessoas'
else:
p1 = 'pessoa'
else:
menor += 1
if menor != 1:
p2 = 'pessoas'
else:
p2 = 'pessoa'
print(f'{maior} {p1} de maior e {menor} {p2} de menor')
If all people are older than 18 (or younger) no attribution to the variable P2 (or P1) will occur but you consider such variables in print. As well as initializing the variables larger, smaller and cont initialize also the variables P1 and P2.
– anonimo
You don’t actually need these variables P1 and P2: https://ideone.com/epxCR0
– hkotsubo
Thank you very much!
– Glebson Santos