Skip to main content
 首页 » 编程设计

python 的生命周期

2025年05月04日36pengyingh

我正在尝试用 Python 创建一个程序,告诉您您已经活了多少天。我现在的代码是:

import datetime 
from datetime import timedelta 
 
year = int(input('Enter the year')) 
month = int(input('Enter the month')) 
day = int(input('Enter the day')) 
date1 = datetime.date(year, month, day) 
 
now = datetime.datetime.now().date() 
 
days = now - date1 
print days 

此时它会打印天数,然后是 0:00:00。例如:5914 天,0:00:00。有谁知道如何摆脱 0:00:00?

请您参考如下方法:

days 是 timedelta对象,所以只需执行 print days.days