468,294 Members | 1,894 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,294 developers. It's quick & easy.

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.  
4 Weeks Ago #1
1 2315
SioSio
240 128KB
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))
3 Weeks Ago #2

Post your reply

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

Similar topics

1 post views Thread by Rob R. Ainscough | last post: by
2 posts views Thread by JB | last post: by
5 posts views Thread by sathya moorthy via .NET 247 | last post: by
3 posts views Thread by Smokey Grindel | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.