473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

if ... else structure to find current age


Hello,
I am a beginner in python and programming in general.
I am trying to use the if...else structure to find the current
age of an individual, giving the current day, month
and year as well as birth day, month and
year. I keep getting a logical error which I am unable to correct.
I would be appreciate help in correcting the error in the structure.
Expand|Select|Wrap|Line Numbers
  1. # variables used in the if ... else structure
  2. birthDay = 19
  3. currentDay = 31
  4. birthMonth = 12
  5. currentMonth = 5
  6. birthYear = 1990
  7. currentYear = 2018
  8.  
  9.  
  10. if current_year >= birth_year:
  11.     if current_month >= birth_month:
  12.         if current_day >= birth_day:
  13.             print(current_year - birth_year)
  14.         else:
  15.             print(current_year - birth_year - 1)
  16.     else:
  17.         print(current_year - birth_year - 1)
  18. else:
  19.     print(current_year - birth_year - 1)
  20.  
  21.  
  22. #The following were my test case:
  23. #current_day = 31, 20, 16, 16, 16, 16, 6, 14, 31
  24. #current_month = 5, 8, 6, 6, 6, 6, 10, 6, 5
  25. #current_year = 2018, 2016, 2017, 2017, 2017, 2017, 2016, 2019, 2018
  26. #birth_day = 19, 19, 20, 12, 11, 20, 17, 17, 24
  27. #birth_month = 12, 4, 5, 9, 4, 6, 6, 3, 3
  28. #birth_year = 1990, 1967, 1966, 2009, 1981, 2013, 1951, 2013, 1982
  29.  
  30. #Expected value = 27, 49, 51, 7, 36, 3, 65, 6, 36
  31. #Result = 27, 49, 50, 7, 36, 3, 64, 5, 36
  32.  
Apr 15 '21 #1
1 2996
SioSio
272 256MB
I've included the complete code for you to understand.

This is your code.
Expand|Select|Wrap|Line Numbers
  1. import math
  2. # variables used in the if ... else structure
  3. birth_day = [19, 19, 20, 12, 11, 20, 17, 17, 24]
  4. current_day = [31, 20, 16, 16, 16, 16, 6, 14, 31]
  5. birth_month = [12, 4, 5, 9, 4, 6, 6, 3, 3]
  6. current_month = [5, 8, 6, 6, 6, 6, 10, 6, 5]
  7. birth_year = [1990, 1967, 1966, 2009, 1981, 2013, 1951, 2013, 1982]
  8. current_year = [2018, 2016, 2017, 2017, 2017, 2017, 2016, 2019, 2018]
  9.  
  10. for i in range(len(birth_day)):
  11.     if current_year[i] >= birth_year[i]:
  12.         if current_month[i] >= birth_month[i]:
  13.             if current_day[i] >= birth_day[i]:
  14.                 print(current_year[i] - birth_year[i])
  15.             else:
  16.                 print(current_year[i] - birth_year[i] - 1)
  17.         else:
  18.             print(current_year[i] - birth_year[i] - 1)
  19.     else:
  20.         print(current_year[i] - birth_year[i] - 1)
Modified code.
Expand|Select|Wrap|Line Numbers
  1. for i in range(len(birth_day)):
  2.     if current_year[i] >= birth_year[i]:
  3.         if current_month[i] > birth_month[i]:
  4.                print(current_year[i] - birth_year[i])
  5.         else:
  6.             if current_month[i] == birth_month[i]:
  7.                 if current_day[i] >= birth_day[i]:
  8.                     print(current_year[i] - birth_year[i])
  9.                 else:
  10.                     print(current_year[i] - birth_year[i] - 1)
  11.             else:
  12.                 print(current_year[i] - birth_year[i] - 1)
  13.     else:
  14.         print( "Not Born yet")
Easier way.
Expand|Select|Wrap|Line Numbers
  1. for i in range(len(birth_day)):
  2.     current = float(current_year[i]*10000+current_month[i]*100+current_day[i])
  3.     birth = float(birth_year[i]*10000+birth_month[i]*100+birth_day[i])
  4.     print(math.floor((current-birth)/10000.0))
Apr 20 '21 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Rob R. Ainscough | last post by:
I'm using VS 2003 VB.NET IDE and the Find (Ctrl+F) function is not working when I select "Search Current Project" -- it will only search the current open file not all the files in the project. ...
2
by: JB | last post by:
I'm using an OpenFileDialog and I want to set the Initial directory to the current users desktop, any quick way to get this? Jason
5
by: sathya moorthy via .NET 247 | last post by:
(Type your message here) hi. i am doing a small application project in c#. can any one help me how to find the directory of the current running program. how to create the input box in c# program. ...
0
by: Saloni | last post by:
I have windows service which is going to run on several machines which are in different time zones in USA. I want to find out the Eastern Zone current time from these different machines. It...
3
by: Smokey Grindel | last post by:
I'm making some custom controls that make use of the current site map, I need to know what node the current page that is loaded is on in the site map though, how do you get this information out of...
1
by: ashokingroups | last post by:
Hi In my program, I need to print the current number of active users at this website, means How many number of people are currently accessing the website. Can anyone please tell the solution: How...
0
parshupooja
by: parshupooja | last post by:
Hi, I want to find out current quater of the year. Can anyone let me know how to find out? Quater1 Jan - Mar Quater2 Apr - jun Quater1 July- sep Quater1 oct- Dec This is urgent, so any...
8
by: John Mott | last post by:
Hi all, Is there a way to get the name of the current class in a static method? I have a base class that contains static methods and that class is inherited. I'd like to change the behavior in...
8
by: neelsfer | last post by:
Some of clients have an backend linked file that needs to be renamed. This file is not the same for my different clients. What i have in mind is to use VBA to find the current backend file name...
0
by: dseals22 | last post by:
How can I get my current content to slide to the left and bring in new content from the right (after clicking on button 1) when an existing student's username and password are correct, and if it's...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.