Python Day 4: Class vs Instance

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:
# yearPasses() should increase the age instance variable by 1.
# amIOld() should perform the following conditional actions:
* If age<13, print You are young..
* If age>=13 and <18, print You are a teenager..
* Otherwise, print You are old..
To help you learn by example and complete this challenge, much of the code is provided for you, but you'll be writing everything in the future. The code that creates each instance of your Person class is in the main method. Don't worry if you don't understand it all quite yet!

  1. class Person:
  2. def __init__(self,initialAge):
  3. # Add some more code to run some checks on initialAge
  4. if initialAge < 0:
  5. print("Age is not valid, setting age to 0.")
  6. self.age = 0
  7. def amIOld(self):
  8. # Do some computations in here and print out the correct statement to the console
  9. if age <13:
  10. print("You are young.")
  11. elif age >= 13 and age <18:
  12. print("You are a teenager.")
  13. else:
  14. print('You are old.')
  15. def yearPasses(self):
  16. # Increment the age of the person in here
  17. global age
  18. age= age+1
  19. t = int(input())
  20. for i in range(0, t):
  21. age = int(input())
  22. p = Person(age)
  23. p.amIOld()
  24. for j in range(0, 3):
  25. p.yearPasses()
  26. p.amIOld()
  27. print("") is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.
