From b5d74c165dcefcfa976e934957a660a329d3d95c Mon Sep 17 00:00:00 2001 From: Rohit Barua <83600150+Rohit-beep-droid@users.noreply.github.com> Date: Sun, 23 May 2021 18:06:33 -0400 Subject: [PATCH] Update 100+ Python challenging programming exercises.txt An easy hackerrank challenge from 30 days of Code. --- ...thon challenging programming exercises.txt | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 97af5aaf..034dd26d 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -2371,5 +2371,47 @@ solutions=solve(numheads,numlegs) print solutions #----------------------------------------# +Level: Easy +Question: +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 > or equal to 13 and age < or equal to 18, print "You are a teenager.". + * Otherwise, print "You are old.". + +(credit: HackerRank (30 days of Code)) + +Answer: + +class Person: + def __init__(self,initialAge): + self.age = initialAge + + def amIOld(self): + if self.age<13: + if self.age<0: + self.age=0 + print("Age is not valid, setting age to 0.") + print("You are young.") + elif 13<=self.age<18: + print("You are a teenager.") + else: + print("You are old.") + + def yearPasses(self): + self.age+=1 + +t = int(input()) +for i in range(0, t): + age = int(input()) + p = Person(age) + p.amIOld() + for j in range(0, 3): + p.yearPasses() + p.amIOld() + print("") +#----------------------------------------#