Programming tutorials Episode 1 - How to calculate your age using python code.

avatar
(Edited)

75392250-1BD1-4A6E-81CF-767F73896A99.png

Designed by me in Canva


Greetings my fellow hivers,

I bring good news to the wonderful people of this community and hive as a whole. For some time now I have been thinking about starting something like an ‘episode’ series of programming tutorials where I would write a code in mostly python to do something.

I am a lvl 400 computer science student, so with the help of what I’ve learnt in school and my personal research, I will be showing you guys some cool ways to use python programs to help make your life easier.

So for this episode, I will be writing a python code to calculate the age of any person. Any time i want to calculate my own age I use my fingers which is very stressful. I don’t know if I’m the only one that usually face this kind of problem. So with this code I’m going to share with you, you wouldn’t have to that anymore. So let’s dive right in.

Before you can do anything I will be doing, I suggest you install the python application PHYCHARM on your laptop or desktop computer. I think I will have to do another episode on how to install the app on your PC.

The first thing we consider to write this code is what will we need to achieve our goal? The first thing is we need to do is ;

  • Ask the user to enter his birth year

  • Ask the user to enter his brith month

  • Ask the user to enter the day he was born (birth day ).

I believe these are what we need the user to enter for the code to calculate the age. Now let’s go the app.

For the first function year, in other to allow the user to enter his birth year, you need to type int(input(“Enter your birth year “). The INT function allows us to enter integers since it’s going to be a number.

If you should run this code at this stage, this is what you get

After defining the functions that will allow the user to input, we need to define the functions the computer will also use to calculate the age.

The above functions defined, birthday, today and age. Is what the computer will use to determine the age. But if you look closely it has underlined date.time this is because it doesn’t recognize it. So we need to import it to the program. We do this by going to the first line of the code and type import datetime

After importing, the underlying should disappear because it has now recognized that function.

What remaining is to Print out the users age. We’ve already defined it to the computer, here is how you do it.

After defining the required functions, in other to print the users age out you need to type print(f” you are {age} years old).

Now let’s run the code to see it’s output.


after entering 10th, October 1999 born on the 10th which is 24th years old.

As you can see it has correctly calculated our age for us. The code is still not perfect, why? This is because what if a user makes a mistake to at the month side he enters 13? We need the code to tell him he has made a mistake and needs to correct it. Here is an adjustment you can make to the code.

All you need to do is add an IF Clause after line 4 or the month you’ve just defined. Let’s see image below

If you do that without adding sys.exit(), the next lines of code will still be executed which wouldn’t be nice.

Result of the code



As you can see it after entering month 13, it prompted the user he made a mistake

We can also make similar adjustments to the where the user enters his day of birth. So that Incase he enters a number above 31 it will alert him to change it. It’s the same if clause we add to the function after day.

Now final output of the code ;

That’s how we write a code in python to calculate one’s age, I hope you loved it. This is just the first of many in our next episode I will show you how to install the phycharm application on your desktop. Until then see you later. Below is the code you can try your hands on it. Thank you

1. import sys
2. import datetime
3. year = int(input("Enter your birth year: "))
4. month = int(input("Enter your birth month: "))
5. if month > 12:
6. print("you have entered the wrong number, must be between (1-12)")
7. sys.exit()
8. day = int(input("Enter your birth day: "))
9. if day > 31:
10. print("you have entered the wrong day, must be between (1-31)")
11. sys.exit()
12. birthday = datetime.datetime(year, month, day)
13. today = datetime.datetime.today()
14. age = today.year - birthday.year
15. print(f"you are {age} years old")



0
0
0.000
9 comments
avatar
Thank you for sharing this post in the DIYHUB Community!

Your content got selected by our fellow curator stevenson7 & you just received a little thank you upvote from us for your great work! Your post will be featured in one of our recurring compilations which are aiming to offer you a stage to widen your audience within the DIY scene of Hive. Stay creative & HIVE ON!


Please vote for our hive witness <3

0
0
0.000
avatar

Dear @jimah1k,
Our previous proposal expired end of December and the Hivebuzz project is not funded anymore. May we ask you to review and support our new proposal (https://peakd.com/me/proposals/248)?
Thank you for your help!

0
0
0.000
avatar

Wow that's a really great explanation, into details I tried to learn such basic things on the web but the explanations were too shallow and vague. I really find it simple to understand your work. Hope you drop more lessons. 🙏

0
0
0.000
avatar

Thanks bro. I’m glad it was to your understanding. Frankly i taught it was somewhat complex. I’m happy you understood.

0
0
0.000