1

I'm making a simple program that outputs a person's age and days until their birthday and I've run into two problems. My if statement to determine if the birthdate has passed is always using the if portion, not the elif. My if statement at the bottom to display either birthday passed, birthday today, birthday tomorrow, or days until birthday is always using my else statement.

        day_after = birth_date + timedelta(days=1)
        day_before = birth_date - timedelta(days=1)

# enter code to calc age here
        if birth_date.month and birth_date.day > current_date.month and current_date.day:
            year_old = current_date.year - birth_date.year
        elif birth_date.month and birth_date.day < current_date.month and current_date.day:
            year_old = current_date.year - birth_date.year - 1

        # code to calc and print days until persons next birthday
        days_until_birthday = days_until(birth_date, current_date)


        #outputs
        print("Birthday: ", birth_date.strftime("%A, %B %d, %Y"))
        print("Today:\t", day_only.strftime("%A, %B %d, %Y"))
        print(name, "is", year_old, "years old.")
        if current_date.day and current_date.month == day_before.day and day_before.month : # birthday is tomorrow
            print(name, "'s birthday is tomorrow!")
        elif current_date.day and current_date.month == day_after.day and day_after.month: # birthday is yesterday
            print(name, "'s birthday was yesterday!")
        elif current_date.day and current_date.month == birth_date.day and birth_date.month: # birthday is today
            print(name, "'s birthday is today!")
        else:
            print(name + "'s birthday is in", days_until_birthday, "days!")

my output is:

Birthday Calculator

Enter name: Matthew
Enter birthday (MM/DD/YY): 07/17/91
Birthday:  Wednesday, July 17, 1991
Today:   Thursday, October 15, 2020
Matthew is 29 years old.
Matthew's birthday is in 274 days!

Continue? (y/n): y

Enter name: Jayden
Enter birthday (MM/DD/YY): 12/19/11
Birthday:  Monday, December 19, 2011
Today:   Thursday, October 15, 2020
Jayden is 9 years old.
Jayden's birthday is in 64 days!

Continue? (y/n): y

Enter name: Testbirth tomorrow
Enter birthday (MM/DD/YY): 10/16/00
Birthday:  Monday, October 16, 2000
Today:   Thursday, October 15, 2020
Testbirth tomorrow is 20 years old.
Testbirth tomorrow's birthday is in 0 days!
Mwheeler91
  • 215
  • 2
  • 8

1 Answers1

0

The condition if birth_date.month and birth_date.day > current_date.month and current_date.day: looks strange.

birth_date.month and birth_date.day is always birth_date.day.

current_date.month and current_date.day is always current_date.day.

Try this:

console.log(         5 && 1); // 1
console.log(         1 && 2); // 2
console.log(        -1 && 3); // 3
console.log(   Math.PI && 4); // 4
console.log("whatever" && 5); // 5
console.log(         0 && 6); // 0 <-- the only exception

So it works as: if birth_date.day > current_date.day: which makes little sense for me.

What did you mean?

Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23
  • 1
    Do I not need to compare both the day and month? the idea behind this section is to subtract 1 if the user's birthday has not passed yet. – Mwheeler91 Oct 15 '20 at 17:12
  • Besides the strange conditions the task with years and month is way more tricky than it looks at fist sight. I think it makes sense to you to dive into it on your own: https://stackoverflow.com/questions/765797/python-timedelta-in-years – Yuri Khristich Oct 15 '20 at 18:40