Hello forum,
so I've been trying to learn python by myself and made up an exercise that I can't handle.
I've been trying to populate a hotel (i.e. list) with a random amount of people (i.e. objects) with 3 attributes (origin, age, gender). I think that part worked. But now I want to categorize them. Namely, I want to count the number of people that are underage (<18) and how many among those are male or female.
I might be messing up the indentation but I don't know.
Thanks for your help in advance!
Best regards,
Lucas
This is my code so far:
so I've been trying to learn python by myself and made up an exercise that I can't handle.
I've been trying to populate a hotel (i.e. list) with a random amount of people (i.e. objects) with 3 attributes (origin, age, gender). I think that part worked. But now I want to categorize them. Namely, I want to count the number of people that are underage (<18) and how many among those are male or female.
I might be messing up the indentation but I don't know.
Thanks for your help in advance!
Best regards,
Lucas
This is my code so far:
import random
guestsUnderage = 0
guestsUnderageMale = 0
guestsUnderageFemale = 0
guestsAdults = 0
class Tenant:
def __init__(self, origin, age, gender):
self.origin = origin
self.age = age
self.gender = gender
def __str__(self):
return '\nOrigin: ' + self.origin + '\nAge: ' + str(self.age) + '\nGender: ' + self.gender
origins = ['Europe', 'Africa', 'Americas', 'Asia', 'Australia']
ages = range(10,70)
genders = ['male', 'female']
hotel = []
for i in range(random.randint(20,40)): #random number of rooms between 20 and 40
tenanti = Tenant(origins[random.randint(0, len(origins) - 1)], str(ages[random.randint(0, len(ages) - 1)]), genders[random.randint(0, len(genders)-1)])
hotel.append(tenanti)
for i in range(len(hotel)):
if int(tenanti.age) < 18:
guestsUnderage += 1
if tenanti.gender == 'male':
guestsUnderageMale += 1
else:
guestsUnderageFemale += 1
else:
guestsAdults += 1
print(guestsUnderage,guestsUnderageFemale, guestsUnderageMale,guestsAdults)
