编辑:请参阅上面的the thread kaya3 mentioned 中的this answer,以获得更一致可靠的方式来做同样的事情。我将在下面留下我的原始答案,因为它有助于理解如何思考这个问题,但请注意,由于Gregorian calendar 的怪癖,我下面的答案可能会在棘手的情况下搞砸,特别是:
能被 4 整除的每一年都是闰年,但能被 100 整除的年份除外,但如果这些百年年能被 400 整除,则它们就是闰年。例如,1700 年、1800 年、和 1900 年不是闰年,但 1600 年和 2000 年是。
原始答案:
您可以尝试使用time 模块:
import time
import datetime
def main(ask_for_hour_and_minute, convert_to_integers):
year, month, day, hour, minute = ask_for_birthday_info(ask_for_hour_and_minute)
calculate_time_since_birth(year, month, day, hour, minute, convert_to_integers)
def ask_for_birthday_info(ask_for_hour_and_minute):
birthday_year = int(input('What year were you born in?\n'))
birthday_month = int(input('What month were you born in?\n'))
birthday_day = int(input('What day were you born on?\n'))
if ask_for_hour_and_minute is True:
birthday_hour = int(input('What hour were you born?\n'))
birthday_minute = int(input('What minute were you born?\n'))
else:
birthday_hour = 0 # set to 0 as default
birthday_minute = 0 # set to 0 as default
return (birthday_year, birthday_month, birthday_day, birthday_hour, birthday_minute)
def calculate_time_since_birth(birthday_year, birthday_month, birthday_day, birthday_hour, birthday_minute, convert_to_integers):
year = 31557600 # seconds in a year
day = 86400 # seconds in a day
hour = 3600 # seconds in a hour
minute = 60 # seconds in a minute
# provide user info to datetime.datetime()
birthdate = datetime.datetime(birthday_year, birthday_month, birthday_day, birthday_hour, birthday_minute)
birthdate_tuple = time.mktime(birthdate.timetuple())
# figure out how many seconds ago birth was
seconds_since_birthday = time.time() - birthdate_tuple
# start calculations
years_ago = seconds_since_birthday // year
days_ago = seconds_since_birthday // day % 365
hours_ago = seconds_since_birthday // hour % 24
minutes_ago = seconds_since_birthday // minute % 60
seconds_ago = seconds_since_birthday % minute
# convert calculated values to integers if convert_to_integers is True
if convert_to_integers is True:
years_ago = int(years_ago)
days_ago = int(days_ago)
hours_ago = int(hours_ago)
minutes_ago = int(minutes_ago)
seconds_ago = int(seconds_ago)
# print calculations
print(f'Your birthday was {years_ago} years, {days_ago}, days, {hours_ago} hours, {minutes_ago} minutes, {seconds_ago} seconds ago.')
# to ask for just the year, month, and day
main(False, False)
# to ask for just the year, month, and day AND convert the answer to integer values
main(False, True)
# to ask for just the year, month, day, hour, and minute
main(True, False)
# to ask for just the year, month, day, hour, and minute AND convert the answer to integer values
main(True, True)
尝试使用描述性变量名称,以便变量有意义,但操作员可能需要一些解释:
10 // 3 # the // operator divides the numerator by the denominator and REMOVES the remainder, so answer is 3
10 % 3 # the % operator divides the numerator by the denominator and RETURNS the remainder, so the answer is 1
理解了操作符之后,剩下的代码应该就有意义了。为了清楚起见,让我们来看看它
- 通过在
ask_for_birthday_info() 函数中询问用户的信息来创建birthdate
- 将用户提供的信息提供给
calculate_time_since_birth()函数
- 将
birthdate 转换为元组并将其存储在birthdate_tuple
- 计算自生日以来经过了多少秒并将其存储在
seconds_since_birthday
- 通过将
seconds_since_birthday 除以year 中的秒数来计算自生日以来已经过去了多少年
- 通过将
seconds_since_birthday 除以day 中的秒数并保留最近的 365 天(即% 365 days_ago)
- 通过将
seconds_since_birthday 除以hour 中的秒数并保留最近的 24 小时(即% 24 hours_ago)
- 通过将
seconds_since_birthday 除以minute 中的秒数并保留最近的 60 分钟(即% 60 在minutes_ago)
- 除以
seconds_since_birthday 并保留最近的 60 秒(即seconds_ago 中的% 60),计算出自生日以来经过了多少秒
然后,我们只需要打印结果:
print(f'Your birthday was {years_ago} years, {days_ago}, days, {hours_ago} hours, {minutes_ago} minutes, {seconds_ago} seconds ago.')
# if you're using a version of python before 3.6, use something like
print('Your birthday was ' + str(years_ago) + ' years, ' + str(days_ago) + ' days, ' + str(hours_ago) + ' hours, ' + str(minutes_ago) + ' minutes, ' + str(seconds_ago) + ' seconds ago.')
最后,您可以添加一些错误检查以确保用户输入有效信息,这样如果他们说他们出生在月份 15 或月份 -2,您的程序会告诉用户他们提供了无效信息回答。例如,您可以在从用户那里获取生日信息之后,但在调用calculate_time_since_birth() 函数之前执行以下操作:
if not (1 <= month <= 12):
print('ERROR! You provided an invalid month!')
return
if not (1 <= day <= 31):
# note this isn't a robust check, if user provides February 30 or April 31, that should be an error - but this won't catch that
# you'll need to make it more robust to catch those errors
print('ERROR! You provided an invalid day!')
return
if not (0 <= hour <= 23):
print('ERROR! You provided an invalid hour!')
return
if not (0 <= minute <= 59):
print('ERROR! You provided an invalid minute!')
return
if not (0 <= second <= 59):
print('ERROR! You provided an invalid second!')
return