30 Days of Code — Day 4

Tyler Carter
2 min readMar 8, 2021
Photo by Maxwell Nelson on Unsplash

As a forcing function to get myself into the habit of writing these articles — I have decided to write an article for each day of the 30 Days of Code challenge from Hacker Rank.

The 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 .
  2. amIOld() should perform the following conditional actions:
    If , print You are young..
    If and , print You are a teenager..
    Otherwise, print You are old..

The Code

The Result

>? 2
>? 12
… You are young.
… You are a teenager.
>? 18
… You are old.
… You are old.

Conclusion

Similar to the day 2 challenge, I did not have much experience with defining a class and how the class worked together with defined functions. This exercise in particular was both challenging and enjoyable as I felt as though I learned quite a bit about programming in general, not just how to perform loops and define functions.

Here is a link to the day_3 answer/challenge I wrote about previously: 30 Days of Code — Day 3.

--

--

Tyler Carter
0 Followers

I am currently an Operations Program Manager for Microsoft. I have been teaching myself Python to use for Data Analytics and Machine Learning.