为什么该代码的年份返回错误?

时间:2019-08-27 02:19:15

标签: python datetime time

我写这个脚本只是为了练习,由于某种原因,客户输入他们的出生年份后返回的年份存在问题。例如,我的朋友出生于1991年,但十月。该代码在1月有效,但在10月有效,它指出那年(1991年)的NYE是1992年我朋友的生日(即1991年10月13日)。请指教!

我尝试阅读本手册一年,但提供的内容不多。我用谷歌搜索,但是这个问题非常模糊。

from datetime import date
from datetime import time
from datetime import datetime
from datetime import timedelta

today = date.today() # date.var returns date
print '\nToday is', (today.strftime('%B')), (today.strftime('%d')), (today.strftime('%Y')) # time formats used to return mo, day, yr

print "\nHello! What is your name?"
user_name = raw_input('--> ')
print '\nHow old are you, %s?' % user_name
user_age = int(raw_input('--> '))

# Python lists start with 0 index so add in a dummy for 0
months = ['Dummy', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
print '\nWhat day, month and year were you born?'
user_day = int(raw_input('Day: ')) # use this line to get index from months list
user_month = months.index(raw_input('Month: '))
user_year = int(raw_input('Year: ')) 
date_birth = user_year, user_month, user_day

print '\nYou were born on', date_birth

NYE_birth = (today.year - user_age, 1, 1)
print 'The New Year\'s Eve of that year was ', NYE_birth
print 'You were born', user_month - 1, 'months and', user_day - 1, 'days after NYE.\n'

# need to include months

ry = 100 - user_age
age_cent = date (int(today.year + ry), int(user_month), int(user_day))
print '\nWow, a long way to go! You will turn 100 years-old on',  age_cent
print '\n'

预期结果应该是NYE与用户的出生年份相同。但是它显示了第二年。

2 个答案:

答案 0 :(得分:0)

IIUC尝试使用:

from datetime import date, time, datetime, timedelta

today = date.today() # date.var returns date
print '\nToday is', (today.strftime('%B')), (today.strftime('%d')), (today.strftime('%Y')) # time formats used to return mo, day, yr

print "\nHello! What is your name?"
user_name = raw_input('--> ')
print '\nHow old are you, %s?' % user_name
user_age = int(raw_input('--> '))

# Python lists start with 0 index so add in a dummy for 0
months = ['Dummy', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
print '\nWhat day, month and year were you born?'
user_day = int(raw_input('Day: ')) # use this line to get index from months list
user_month = months.index(raw_input('Month: '))
user_year = int(raw_input('Year: ')) 
date_birth = user_year, user_month, user_day



print '\nYou were born on', date_birth


NYE_birth = (user_year, 1, 1)
print 'The New Year\'s Eve of that year was ', NYE_birth
print 'You were born', user_month, 'months and', user_day, 'days after NYE.\n'

# need to include months

ry = 100 - user_age
age_cent = date (int(today.year + ry), int(user_month), int(user_day))
print '\nWow, a long way to go! You will turn 100 years-old on',  age_cent
print '\n'

答案 1 :(得分:0)

由于您的朋友出生于1991年10月,所以他现在还只有27岁,因此他的下一个生日将28岁。如果从当年减去27,则为1992。

如果生日在该年晚于运行该程序时,则需要减去额外的年份来说明这一点。

if today.month > user_month or (today.month == user_month and today.day >= user_day):
    NYE_birth = (today.year - user_age, 1, 1)
else:
    NYE_birth = (today.year - user_age - 1, 1, 1)

您需要对世纪年进行类似的调整。

当然,由于您询问他们的出生年份,因此您可以user_year,而不用从年龄开始计算。