我如何根据今天的年龄和日期来计算出生年份?

时间:2018-04-14 18:18:05

标签: python-3.x

Name = input("What is your name?")
Hobby = input("What is your Hobby?")
Color = input("What is your favorite Color?")

print("Ah, so your Name is %s, your Hobby is %s, "
    "and your favorite Color is %s. " % (Name, Hobby, Color))

print("How old are you?")
input("Type your age here:")

您好,我想根据他们的年龄和日期(2018年)计算出生日期。我需要计算2018年 - 这个人的年龄。我怎么做?我只是不能把它弄好吗。

1 个答案:

答案 0 :(得分:0)

要正确估算生日年份,您还需要询问今年是否已经过生日:

BirthdayDone = input("Did you celebrate your birthday this year?").strip().lower() in [
               "yes","1","true"]

print("How old are you?")
age = int(input("Type your age here:"))  
print(f'Your year of birth is: {2018 - age - (0 if BirthdayDone else 1)}')

然后你可以给出正确的出生年份。

input("Did you celebrate your birthday this year?").strip().lower() in ["yes","1","true"]会从答案中删除空格,如果yes1true将其设为True,则其False }。如果获取正确的出生日期为假,则在printstatement中使用此信息将生日减少1。