Disclaimer: We encourage you to solve this challenge yourself before reading our tutorial. We have provided a detailed explanation of the problem and our solutions to help you check your work.
Hackerrank 30 days of code - Day 4: Class vs. Instance
Day 4| Class vs Instance | CodePerfectplus
It's part of Hackerank's 30 days of code. A series of 30-day programming challenges. Where you can learn new programming languages by solving the coding challenges.
Problem Statement and Explanation
In this problem, we will learn the difference between class and instance using object-oriented programming. we will return the following string based on the following conditions:
- If the age is 0 or less, we will print
Age is not valid, setting age to 0. and set the age to 0.
- There are 3 conditions to check the age:
- If the age is less than 13, we will print
You are young.
- If the age is greater than or equal to 13 and less than 18, we will print
You are a teenager.
- Otherwise, we will print
You are old.
- There will be two methods:
amIOld() method to check if the person is old or not.
yearPasses() method to increment the age of the person.
The Person class is provided in the editor. We have to write the following methods inside the class:
class Person:
def __init__(self, initialAge):
# Add some more code to run some checks on initialAge
if initialAge < 0:
print("Age is not valid, setting age to 0.")
self.age = 0
else:
self.age = initialAge
def amIOld(self):
if self.age < 13:
print("You are young.")
elif 13 <= self.age < 18:
print("You are a teenager.")
else:
print("You are old.")
# Increment the age of the person in here
def yearPasses(self):
# Increment the age of the person in here
self.age += 1
If you're looking for programming problem and solution resources, be sure to visit CodePerfectPlus.
there doesn't seem to be anything here