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. -
# variables used in the if ... else structure
-
birthDay = 19
-
currentDay = 31
-
birthMonth = 12
-
currentMonth = 5
-
birthYear = 1990
-
currentYear = 2018
-
-
-
if current_year >= birth_year:
-
if current_month >= birth_month:
-
if current_day >= birth_day:
-
print(current_year - birth_year)
-
else:
-
print(current_year - birth_year - 1)
-
else:
-
print(current_year - birth_year - 1)
-
else:
-
print(current_year - birth_year - 1)
-
-
-
#The following were my test case:
-
#current_day = 31, 20, 16, 16, 16, 16, 6, 14, 31
-
#current_month = 5, 8, 6, 6, 6, 6, 10, 6, 5
-
#current_year = 2018, 2016, 2017, 2017, 2017, 2017, 2016, 2019, 2018
-
#birth_day = 19, 19, 20, 12, 11, 20, 17, 17, 24
-
#birth_month = 12, 4, 5, 9, 4, 6, 6, 3, 3
-
#birth_year = 1990, 1967, 1966, 2009, 1981, 2013, 1951, 2013, 1982
-
-
#Expected value = 27, 49, 51, 7, 36, 3, 65, 6, 36
-
#Result = 27, 49, 50, 7, 36, 3, 64, 5, 36
-
1 2996
I've included the complete code for you to understand.
This is your code. - import math
-
# variables used in the if ... else structure
-
birth_day = [19, 19, 20, 12, 11, 20, 17, 17, 24]
-
current_day = [31, 20, 16, 16, 16, 16, 6, 14, 31]
-
birth_month = [12, 4, 5, 9, 4, 6, 6, 3, 3]
-
current_month = [5, 8, 6, 6, 6, 6, 10, 6, 5]
-
birth_year = [1990, 1967, 1966, 2009, 1981, 2013, 1951, 2013, 1982]
-
current_year = [2018, 2016, 2017, 2017, 2017, 2017, 2016, 2019, 2018]
-
-
for i in range(len(birth_day)):
-
if current_year[i] >= birth_year[i]:
-
if current_month[i] >= birth_month[i]:
-
if current_day[i] >= birth_day[i]:
-
print(current_year[i] - birth_year[i])
-
else:
-
print(current_year[i] - birth_year[i] - 1)
-
else:
-
print(current_year[i] - birth_year[i] - 1)
-
else:
-
print(current_year[i] - birth_year[i] - 1)
Modified code. - for i in range(len(birth_day)):
-
if current_year[i] >= birth_year[i]:
-
if current_month[i] > birth_month[i]:
-
print(current_year[i] - birth_year[i])
-
else:
-
if current_month[i] == birth_month[i]:
-
if current_day[i] >= birth_day[i]:
-
print(current_year[i] - birth_year[i])
-
else:
-
print(current_year[i] - birth_year[i] - 1)
-
else:
-
print(current_year[i] - birth_year[i] - 1)
-
else:
-
print( "Not Born yet")
Easier way. - for i in range(len(birth_day)):
-
current = float(current_year[i]*10000+current_month[i]*100+current_day[i])
-
birth = float(birth_year[i]*10000+birth_month[i]*100+birth_day[i])
-
print(math.floor((current-birth)/10000.0))
Sign in to post your reply or Sign up for a free account.
Similar topics
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. ...
|
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
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |