print (yourname1)
snum = input('What is your date of birth (YYYYMMDD)?:')
birth1 = snum[4:6] + '/' +snum[6:8] + '/' + snum[0:4]
y1 = int(snum[0:4]) #year of person 1
m1 = int(snum[4:6]) #month ""
d1 = int(snum[6:8]) #day '"
print ('\n')
print ('\n')
sleep(1)
print (yourname2)
snum = input('What is your date of birth (YYYYMMDD)?:')
birth2 = snum[4:6] + '/' +snum[6:8] + '/' + snum[0:4]
y2 = int(snum[0:4]) #year of person 2
m2 = int(snum[4:6]) #month ""
d2 = int(snum[6:8]) #day ""
print ('\n')
print ('\n')
sleep(1)
print (birth1,', am I correct,', yourname1,'?')
sleep(1)
print ('\n')
print ('\n')
sleep(1)
print (birth2,', am I correct,', yourname2,'?')
sleep(1)
print ('\n')
print ('\n')
sleep(1)
def compare(y1, y2):
    if y2 < y1:
        print (yourname2, "is older than", yourname1)
    elif y1 < y2:
        print (yourname1, "is older than", yourname2)
    else:
        print (yourname1, "and", yourname2, "are the same age!")
compare(y1, y2)

This will effectively decide who is older by using the year alone. However, if a pair of people were born the same year but different month/day or same month/different day how do i tell python to compare all those factors ...

*this is a piece of code from larger program

Recommended Answers

All 4 Replies

There is a trick, you can compare tuples, for example

>>> (1980, 3, 15) < (1987, 7, 12)
True
>>> (1987, 3, 15) < (1987, 7, 12)
True
>>> (1987, 7, 15) < (1987, 7, 12)
False

So, instead of passing the years of birth to your function compare, you only need to pass triples (year, month, day) !

er
I kind of see what youre saying, but could you elaborate... how would I fit this into my code?

*Please note, it is required that I ask the user for their birthday in YYYYMMDD format, which I convert to MM/DD/YYYY format, which I then use to compare to see who is older..

Well, you simply replace the last line of your code above by

compare( (y1, m1, d1), (y2, m2, d2) )

You have (y1, m1, d1) < (y2, m2, d2) if y1 is before y2 or if y1 is equal to y2 but m1 is before m2 or if y1 = y2 and m1 = m2 and d1 is before d2.

oh wow, thank you sir.. that is perfect!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.