Skip to main content
 首页 » 编程设计

python之停留在以 yyyy/mm/dd 形式比较出生日期

2025年05月04日17小虾米

我是新手,想制作一个比较出生日期的简单程序。 到目前为止我有这段代码

y1 = int(input("Enter the year of birth of person 1 in the form of YYYY"+" ")) 
 
m1 =int(input("Enter the month of birth of person 1 in the form of MM"+" "))  
 
d1 =int(input("Enter the day of birth of person 1 in the form of DD"+" ")) 
 
y2 = int(input("Enter the year of birth of person 2 in the form of YYYY"+" ")) 
 
m2 =int(input("Enter the month of birth of person 2 in the form of MM"+" "))  
 
d2 =int(input("Enter the day of birth of person 2 in the form of DD"+" ")) 

只要出生年份不同,我就可以成功比较出生日期。我不知道如何编写代码,以便如果出生年份相同,它将比较出生月份。如果出生月份是相同的,它将比较出生日期并相应地打印出来。尝试在谷歌上搜索但没有找到这个特定问题的运气,只有类似的问题。

请记住,我几乎不知道如何打开 Python Gui Idle 并保存我的项目,所以我无法理解困难的答案,尤其是当它们引用了更深入的 Python 知识时。

请您参考如下方法:

你可以 compare tuples所有三个值!

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

首先,这会检查是否 y1 < y2 .如果它们相等,它检查是否 m1 < m2 , 等等。

>>> (2001, 3, 13) < (2002, 3, 14) 
True 
>>> (2001, 3, 13) < (2001, 3, 12) 
False