Python 需要向年龄计算器添加月份的帮助吗
我正在为我的CS课程创建一个年龄计算器,我需要添加一个公式,在这里你可以用年和月来显示一个人的年龄 年公式很简单,只需现在做即可。然而,年-年-月要复杂得多,因为python会将两个月之间的距离计算为负值。例如,如果您输入的月份是2月,而不是说您离生日还有8个月,则表示您离生日还有-3个月Python 需要向年龄计算器添加月份的帮助吗,python,python-3.x,Python,Python 3.x,我正在为我的CS课程创建一个年龄计算器,我需要添加一个公式,在这里你可以用年和月来显示一个人的年龄 年公式很简单,只需现在做即可。然而,年-年-月要复杂得多,因为python会将两个月之间的距离计算为负值。例如,如果您输入的月份是2月,而不是说您离生日还有8个月,则表示您离生日还有-3个月 import datetime now = datetime.datetime.now() print ("Current year:", now.year) print ("Current month:",
import datetime
now = datetime.datetime.now()
print ("Current year:", now.year)
print ("Current month:", now. month)
year = int(input("What year were you born in? "))
month = int(input("What month were you born in? "))
agemonth = month - now.month
age = now.year - year
print("Your age is", age)
if age >= 16:
print("You are old enough to drive")
else:
print("You are not old enough to drive")
if age >= 21:
print("You are old enough to drink")
else:
print("You are not old enough to drink")
理想情况下,如果您输入的是2月而不是3个月,则程序将打印您离生日还有8个月尝试使用模运算符:
agemonth = (month - now.month) % 12
当你出生在一月,现在是二月,减法得到-1,但12的余数是11,这就是我们想要的。要处理负月份,你需要做两件事 在月数的基础上加上12,使其为正,或者取%12,这样-4变成8 每年减少1 您还可以简化后一个if语句 考虑到这两个变化,代码变为
import datetime
now = datetime.datetime.now()
print ("Current year:", now.year)
print ("Current month:", now. month)
year = int(input("What year were you born in? "))
month = int(input("What month were you born in? "))
agemonth = now.month - month
age = now.year - year
#If months are negative
if agemonth < 0:
#Take modulo 12 of agemonth to make it positive
agemonth = agemonth%12
#Subtract 1 from year
age = age - 1
print("Your age is ", age, "years and", agemonth, 'months')
#3 If statements based on ages
if age <= 16:
print("You are not old enough to drive")
print("You are not old enough to drink")
elif 16 < age < 21:
print("You are old enough to drive")
print("You are not old enough to drink")
else:
print("You are old enough to drive")
print("You are old enough to drink")
考虑到您所说的某人的生日在2月,输出应该表明他们的生日还有8个月,正确的公式应该是:
agemonth = (month-now.month-1)%12
#month = 2, now.month = 5. Thus agemonth = 8
这将始终为您提供所需的输出
from datetime import date
year = int(input("What year were you born in? "))
month = int(input("What month were you born in? "))
today = date.today()
age = today.year - year
agemonth = today.month - month
totmonth = 0
if agemonth >= 0:
totmonth = agemonth
else:
agemonth = 12 + agemonth
age -= 1
print "age is" , age,"years and", agemonth,"months"
if age <= 16:
print("You are not old enough to drive")
print("You are not old enough to drink")
elif 16 < age < 21:
print("You are old enough to drive")
输出:
你是哪一年出生的?一九九九年
你出生在哪个月?七,
年龄为19岁11个月
你已经足够大了,可以开车了
你还没到喝酒的年龄
如果用一个if来检查月份是否为负值,如果是,请修复它。你需要处理你的if条件,将负月份加12,将年份减1,检查我下面的答案@ergofye:
from datetime import date
year = int(input("What year were you born in? "))
month = int(input("What month were you born in? "))
today = date.today()
age = today.year - year
agemonth = today.month - month
totmonth = 0
if agemonth >= 0:
totmonth = agemonth
else:
agemonth = 12 + agemonth
age -= 1
print "age is" , age,"years and", agemonth,"months"
if age <= 16:
print("You are not old enough to drive")
print("You are not old enough to drink")
elif 16 < age < 21:
print("You are old enough to drive")