1. 先获取用户的出生日期
  2. 计算出生日期到当前日期的天数,即为活了多少天
  3. 计算当前年份减去出生年份,即为目前多少岁

代码实现:

import datetime

# 获取用户出生日期
year = int(input("请输入出生年份:"))
month = int(input("请输入出生月份:"))
day = int(input("请输入出生日期:"))

# 计算活了多少天
birthday = datetime.date(year, month, day)
today = datetime.date.today()
days_alive = (today - birthday).days
print("你已经活了{}天".format(days_alive))

# 计算目前多少岁
age = today.year - year
print("你目前{}岁".format(age))
分析以下需求并完成代码 请使用代码实现计算你活了多少天目前多少岁

原文地址: https://www.cveoy.top/t/topic/cfrW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录