创建一个函数,在使用python输入DOB时以年为单位打印年龄

创建一个函数,在使用python输入DOB时以年为单位打印年龄,python,python-3.x,Python,Python 3.x,经过一百次尝试,我终于让它工作了,但我想知道是否有其他方法来编写此代码,我是python的新手,如果有任何帮助,我将不胜感激 import datetime date= datetime.datetime.now() def age_calc(age): age = date.year - int(dob) return age dob=input("Please Enter DOB here: ") print("You are %s years old" % age_ca

经过一百次尝试,我终于让它工作了,但我想知道是否有其他方法来编写此代码,我是python的新手,如果有任何帮助,我将不胜感激

import datetime

date= datetime.datetime.now()
def age_calc(age):
    age = date.year - int(dob)
    return age
dob=input("Please Enter DOB here: ")

print("You are %s years old" % age_calc(dob))

即使是像这样的一个小项目,也总有需要改进和学习的地方。一些建议:

  • 变量名称要明确,并且要正确!您将年龄作为
    age\u calc
    的参数,但这实际上是出生日期。每个人都知道什么是dob,不过最好还是把它说清楚,尤其是在断章取义的情况下
  • 计算不正确,我出生于1983年11月5日,因此仍然35岁,而不是你的程序告诉我的36岁
  • 你要求的是出生日期,但事实上你要求的是出生年份,总是很好地说明你需要什么格式
  • 对手动输入进行一些错误检查是一种很好的做法
  • 使用
    if uuuuu name_uuu='\uuuuu main\uuuuu':
    构造程序并将其拆分为多个函数,每个函数完成一件事
  • 我个人喜欢使用f-strings
    f“Hello{name}”
看一看下面的重构代码来说明这一点

import datetime

def age_calc(date_of_birth):
    current_date = datetime.datetime.now()
    age_days = current_date - date_of_birth
    age_years = int(age_days.days / 365.25)
    return age_years

def get_date_of_birth():
    answered = False
    while not answered:
        dob_answer = input("Please Enter DOB here (DD-MM-YYYY) (enter q to quit): ")

        if dob_answer in ['q', 'Q']:
            exit()

        try:
            date_of_birth = datetime.datetime.strptime(dob_answer, "%d-%m-%Y")
            answered = True

        except ValueError:
            print(f"{dob_answer} has incorrect format, use: d-m-yyyy")

    return date_of_birth

def print_age(date_of_birth):
    print(f"You are {age_calc(date_of_birth)} years old")

if __name__ == '__main__':
    while True:
        dob = get_date_of_birth()
        print_age(dob)

祝你好运

即使是像这样的一个小程序,也总有需要改进和学习的地方。一些建议:

  • 变量名称要明确,并且要正确!您将年龄作为
    age\u calc
    的参数,但这实际上是出生日期。每个人都知道什么是dob,不过最好还是把它说清楚,尤其是在断章取义的情况下
  • 计算不正确,我出生于1983年11月5日,因此仍然35岁,而不是你的程序告诉我的36岁
  • 你要求的是出生日期,但事实上你要求的是出生年份,总是很好地说明你需要什么格式
  • 对手动输入进行一些错误检查是一种很好的做法
  • 使用
    if uuuuu name_uuu='\uuuuu main\uuuuu':
    构造程序并将其拆分为多个函数,每个函数完成一件事
  • 我个人喜欢使用f-strings
    f“Hello{name}”
看一看下面的重构代码来说明这一点

import datetime

def age_calc(date_of_birth):
    current_date = datetime.datetime.now()
    age_days = current_date - date_of_birth
    age_years = int(age_days.days / 365.25)
    return age_years

def get_date_of_birth():
    answered = False
    while not answered:
        dob_answer = input("Please Enter DOB here (DD-MM-YYYY) (enter q to quit): ")

        if dob_answer in ['q', 'Q']:
            exit()

        try:
            date_of_birth = datetime.datetime.strptime(dob_answer, "%d-%m-%Y")
            answered = True

        except ValueError:
            print(f"{dob_answer} has incorrect format, use: d-m-yyyy")

    return date_of_birth

def print_age(date_of_birth):
    print(f"You are {age_calc(date_of_birth)} years old")

if __name__ == '__main__':
    while True:
        dob = get_date_of_birth()
        print_age(dob)

祝你好运

没有太多其他方法可以编写输入提示和减法并打印语句。。。你到底期望什么?没有太多其他方法来编写输入提示和减法并打印语句。。。你到底在期待什么?