The one-stop spot for beginners.

Task
Write a Person class with an instance variable, age, and a constructor that takes an integer, initialAge, as a parameter. The constructor must assign initialAge to age after confirming the argument passed as initialAge is not negative; if a negative argument is passed as initialAge, the constructor should set age to 0 and print Age is not valid, setting age to 0.. In addition, you must write the following instance methods:

  1. yearPasses() should increase the instance variable by 1.
  2. amIOld() should perform the following conditional actions:
  • If age<13, print You are young..
  • If age≥13 and age<18, print You are a teenager..
  • Otherwise, print You are old.

Solution

class Person:#instance variable
age = 0
#if the initialAge is less than 0 print statement. If it is greater than 0 set age = initialAge. def __init__(self,initialAge):
if initialAge <0:
print('Age is not valid, setting age to 0.')
else:
self.age = initialAge
#if the age is less than 13 print ('You are young.')
#if it is equal to 13 and less than 18 print ('You are a teengaer.')
#all remaining ages print ('You are old.')
def amIOld(self):
if self.age < 13:
print ('You are young.')
elif self.age >= 13 and self.age <18:
print ('You are a teenager.')
else:
print ('You are old.')
#add 1 year to age. def yearPasses(self):
self.age +=1

Thanks for stopping by. If you want to see more solutions share and clap for more!

--

--