create account

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

View this thread on: hive.blogpeakd.comecency.com
· @jimah1k · (edited)
$32.66
Programming tutorials Episode 1 - How to calculate your age using python code.
<div class="text-justify">




![75392250-1BD1-4A6E-81CF-767F73896A99.png](https://images.hive.blog/DQmSoDsJkCTtUC4iQnBA4jWAaLkcjJWX63CqYaUaHzTZe7u/75392250-1BD1-4A6E-81CF-767F73896A99.png)

<center><sub>Designed by me in Canva</sub></center>

*******

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. 

![](https://images.ecency.com/DQmb7CejDK5r8yURWhwh2vBDeE8UEfuptwPPBxxfmUPEbDG/img_6918.jpg)

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 


![](https://images.ecency.com/DQmUc3QiDvRAv1UrCy6eWb5vq6TZQv3TJJPbZidcNpRa1DV/img_6909.png)


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. 


![](https://images.ecency.com/DQmTrthBgyA6aYo16zg5DGmkngiAH5sacLJ9vvMyjpyFWLE/img_6919.jpg)

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** 


![](https://images.ecency.com/DQmQxMtbSQGS4Q4aJWS3ES24AvW8UErZXPrHcPMKACtgd9q/img_6920.jpg)

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

![](https://images.ecency.com/DQmW2Z6Q75WoRwYtxb2i4d4ywGSb43VPn3XdcxtzT6D1khe/img_6921.jpg)



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


![](https://images.ecency.com/DQmeSBNkxtotCcYYFaRdb8zWt5r827PVHjLsPXBJCeeVDgN/img_6922.jpg)

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. 

![](https://images.ecency.com/DQmQseCCnKWJfpX5Qw45aodRKZqmKZW7piMLEPQJsXaq9Zr/img_6903.png)
<center><sub>after entering 10th, October 1999 born on the 10th which is 24th years old.</sub></center>

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 

![](https://images.ecency.com/DQmWzCRkfSdzMGaa63jxiApNCUMGX6YohLPQvMWgSdHHJop/img_6901.png)

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
******

![](https://images.ecency.com/DQmXRfC3r3aDedfht9EwyLhpLzoewJSr5oShhkabjK2A93F/img_6899.png)
<center><sub>As you can see it after entering month 13, it prompted the user he made a mistake</sub></center>

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. 


![](https://images.ecency.com/DQmevNr6ZdGpdgqf9MBo3SqsxEXFHzhFpiTr9uJFu8FZL1k/img_6898.png)

Now final output of the code ;


![](https://images.ecency.com/DQmPWPy84A8R7uTHc85gmDNaio6Ysx3NMT3iHm3YvAdVdDc/img_6917.png)


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 

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

</div>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 17 others
👎  
properties (23)
authorjimah1k
permlinkprogramming-tutorials-episode-1-how
categoryhive-189641
json_metadata{"image":["https://images.hive.blog/DQmSoDsJkCTtUC4iQnBA4jWAaLkcjJWX63CqYaUaHzTZe7u/75392250-1BD1-4A6E-81CF-767F73896A99.png","https://images.ecency.com/DQmb7CejDK5r8yURWhwh2vBDeE8UEfuptwPPBxxfmUPEbDG/img_6918.jpg","https://images.ecency.com/DQmUc3QiDvRAv1UrCy6eWb5vq6TZQv3TJJPbZidcNpRa1DV/img_6909.png","https://images.ecency.com/DQmTrthBgyA6aYo16zg5DGmkngiAH5sacLJ9vvMyjpyFWLE/img_6919.jpg","https://images.ecency.com/DQmQxMtbSQGS4Q4aJWS3ES24AvW8UErZXPrHcPMKACtgd9q/img_6920.jpg","https://images.ecency.com/DQmW2Z6Q75WoRwYtxb2i4d4ywGSb43VPn3XdcxtzT6D1khe/img_6921.jpg","https://images.ecency.com/DQmeSBNkxtotCcYYFaRdb8zWt5r827PVHjLsPXBJCeeVDgN/img_6922.jpg","https://images.ecency.com/DQmQseCCnKWJfpX5Qw45aodRKZqmKZW7piMLEPQJsXaq9Zr/img_6903.png","https://images.ecency.com/DQmWzCRkfSdzMGaa63jxiApNCUMGX6YohLPQvMWgSdHHJop/img_6901.png","https://images.ecency.com/DQmXRfC3r3aDedfht9EwyLhpLzoewJSr5oShhkabjK2A93F/img_6899.png","https://images.ecency.com/DQmevNr6ZdGpdgqf9MBo3SqsxEXFHzhFpiTr9uJFu8FZL1k/img_6898.png","https://images.ecency.com/DQmPWPy84A8R7uTHc85gmDNaio6Ysx3NMT3iHm3YvAdVdDc/img_6917.png"],"tags":["programming","python","code","diyhub","aliveandthriving","neoxian","pob"],"app":"hiveblog/0.1","format":"markdown"}
created2023-01-11 11:31:27
last_update2023-01-11 11:41:24
depth0
children9
last_payout2023-01-18 11:31:27
cashout_time1969-12-31 23:59:59
total_payout_value16.334 HBD
curator_payout_value16.321 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,975
author_reputation56,205,977,380,039
root_title"Programming tutorials Episode 1 - How to calculate your age using python code."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,789,328
net_rshares65,681,962,894,192
author_curate_reward""
vote details (82)
@abduljawad002 ·
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. 🙏
properties (22)
authorabduljawad002
permlinkre-jimah1k-2023113t18394110z
categoryhive-189641
json_metadata{"tags":["hive-189641","programming","python","code","diyhub","aliveandthriving","neoxian","pob"],"app":"ecency/3.0.35-mobile","format":"markdown+html"}
created2023-01-13 18:39:06
last_update2023-01-13 18:39:06
depth1
children1
last_payout2023-01-20 18:39:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length225
author_reputation3,881,596,896,505
root_title"Programming tutorials Episode 1 - How to calculate your age using python code."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,853,953
net_rshares0
@jimah1k ·
Thanks bro. I’m glad it was to your understanding. Frankly i taught it was somewhat complex. I’m happy you understood. 
properties (22)
authorjimah1k
permlinkre-abduljawad002-2023113t184419311z
categoryhive-189641
json_metadata{"tags":["hive-189641","programming","python","code","diyhub","aliveandthriving","neoxian","pob"],"app":"ecency/3.0.35-mobile","format":"markdown+html"}
created2023-01-13 18:44:18
last_update2023-01-13 18:44:18
depth2
children0
last_payout2023-01-20 18:44:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length119
author_reputation56,205,977,380,039
root_title"Programming tutorials Episode 1 - How to calculate your age using python code."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,854,074
net_rshares0
@diyhub ·
##### Thank you for sharing this post in the [DIYHUB Community](https://peakd.com/c/hive-189641/created)!
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!

<center><a href="https://peakd.com/trending/hive-189641"><img src="https://files.peakd.com/file/peakd-hive/diyhub/23tcEcHsXzT1g5V7Ub8pC5vGQzQnrdPURcYsxxVDDtD4atSyyPJufncjzVKchNwCNugoi.png"></a>
<a href="https://hivesigner.com/sign/account-witness-vote?witness=diyhub&approve=1">Please vote for our hive witness <3</a></center>
properties (22)
authordiyhub
permlinkre-programming-tutorials-episode-1-how-20230111t121506z
categoryhive-189641
json_metadata"{"app": "beem/0.24.26"}"
created2023-01-11 12:15:06
last_update2023-01-11 12:15:06
depth1
children1
last_payout2023-01-18 12:15:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length747
author_reputation529,843,081,803,354
root_title"Programming tutorials Episode 1 - How to calculate your age using python code."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,790,010
net_rshares0
@jimah1k ·
Thank you very much. 
properties (22)
authorjimah1k
permlinkre-diyhub-2023111t131732430z
categoryhive-189641
json_metadata{"tags":["ecency"],"app":"ecency/3.0.35-mobile","format":"markdown+html"}
created2023-01-11 13:17:33
last_update2023-01-11 13:17:33
depth2
children0
last_payout2023-01-18 13:17:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length21
author_reputation56,205,977,380,039
root_title"Programming tutorials Episode 1 - How to calculate your age using python code."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,791,125
net_rshares0
@ecency ·
**Yay!** 🤗<br>Your content has been **boosted with Ecency Points**, by @jimah1k. <br>Use Ecency daily to boost your growth on platform! <br><br><b>Support Ecency</b><br>[Vote for new Proposal](https://hivesigner.com/sign/update-proposal-votes?proposal_ids=%5B245%5D&approve=true)<br>[Delegate HP and earn more](https://ecency.com/hive-125125/@ecency/daily-100-curation-rewards)
properties (22)
authorecency
permlinkre-2023111t173645585z
categoryhive-189641
json_metadata{"tags":["ecency"],"app":"ecency/3.0.20-welcome","format":"markdown+html"}
created2023-01-11 17:36:51
last_update2023-01-11 17:36:51
depth1
children0
last_payout2023-01-18 17:36:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length377
author_reputation613,589,286,978,643
root_title"Programming tutorials Episode 1 - How to calculate your age using python code."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,797,428
net_rshares0
@hivebuzz ·
Dear @jimah1k,<br>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)?<br>Thank you for your help!
properties (22)
authorhivebuzz
permlinktxt-jimah1k-20230113t072324
categoryhive-189641
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2023-01-13 07:23:24
last_update2023-01-13 07:23:24
depth1
children3
last_payout2023-01-20 07:23:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length231
author_reputation369,203,081,481,287
root_title"Programming tutorials Episode 1 - How to calculate your age using python code."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,839,642
net_rshares0
@jimah1k ·
Ok. Will surely do. 
properties (22)
authorjimah1k
permlinkre-hivebuzz-2023113t161434404z
categoryhive-189641
json_metadata{"tags":["ecency"],"app":"ecency/3.0.35-mobile","format":"markdown+html"}
created2023-01-13 16:14:33
last_update2023-01-13 16:14:33
depth2
children2
last_payout2023-01-20 16:14:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length20
author_reputation56,205,977,380,039
root_title"Programming tutorials Episode 1 - How to calculate your age using python code."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,850,971
net_rshares0
@hivebuzz ·
Thank you. Looking forward to getting your support for [our proposal](https://peakd.com/me/proposals/248) 🙂⏳
All you need to do is to click on the "support" button on this page: https://peakd.com/proposals/248. It won't cost you anything!<div><a href="https://engage.hivechain.app">![](https://i.imgur.com/XsrNmcl.png)</a></div>
properties (22)
authorhivebuzz
permlinkre-1673678108325
categoryhive-189641
json_metadata{"app":"engage"}
created2023-01-14 06:35:09
last_update2023-01-14 06:35:09
depth3
children1
last_payout2023-01-21 06:35:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length328
author_reputation369,203,081,481,287
root_title"Programming tutorials Episode 1 - How to calculate your age using python code."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,867,277
net_rshares0