Python 带有DateTime和计算生日是否已过的If语句

Python 带有DateTime和计算生日是否已过的If语句,python,Python,我正在制作一个简单的程序,输出一个人的年龄和生日前的天数,我遇到了两个问题。确定生日是否已过的if语句总是使用if部分,而不是elif。底部显示生日已过、今天生日、明天生日或生日前几天的if语句始终使用我的else语句 day_after = birth_date + timedelta(days=1) day_before = birth_date - timedelta(days=1) # enter code to calc age here

我正在制作一个简单的程序,输出一个人的年龄和生日前的天数,我遇到了两个问题。确定生日是否已过的if语句总是使用if部分,而不是elif。底部显示生日已过、今天生日、明天生日或生日前几天的if语句始终使用我的else语句

        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!")

如果出生日期.月份和出生日期.日期>当前日期.月份和当前日期.日期:的条件看起来很奇怪

birth\u date.month和birth\u date.day
始终是
birth\u date.day

current_date.month和current_date.day
始终是
current_date.day

试试这个:

console.log(5&&1);//1.
console.log(1和2);//2.
console.log(-1&&3);//3.
console.log(Math.PI&&4);//4.
console.log(“无论什么”和&5);//5.

console.log(0&&6);//0如果出生日期.月份和出生日期.日期>当前日期.月份和当前日期.日期:
看起来很奇怪

birth\u date.month和birth\u date.day
始终是
birth\u date.day

current_date.month和current_date.day
始终是
current_date.day

试试这个:

console.log(5&&1);//1.
console.log(1和2);//2.
console.log(-1&&3);//3.
console.log(Math.PI&&4);//4.
console.log(“无论什么”和&5);//5.

console.log(0&&6);//0我不需要同时比较日和月吗?这一部分的想法是,如果用户的生日还没有过,就减去1。除了奇怪的条件外,年和月的任务比第一眼看到的要复杂得多。我认为你自己深入研究一下是有道理的:我不需要比较一天和一个月吗?这一部分的想法是,如果用户的生日还没有过,就减去1。除了奇怪的条件外,年和月的任务比第一眼看到的要复杂得多。我认为你有理由自己深入研究:
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!