我在做一个问题,我需要知道用户是否足够大或不喝酒。用户输入他们的生日,他们必须在1993年2月或之前出生才合法。这是我到目前为止所得到的。
#User inputs their birthday.
year = int(input("Enter your birth year(as a number): "))
month = int(input("Enter your birth month(as a number): "))
#Finding out if they are old enough to drink or not
found=False
if (year<=1993):
found ==True
print("You are old enough to drink in Washington")
elif (month<=2):
found ==True
print("You are old enough to drink in Washington")
elif (month>=3):
found ==True
print("You aren't 21 yet")
当我输入1993年时,3。它会说你已经够大了,不应该喝酒。
答案 0 :(得分:2)
你应该追溯你的陈述的逻辑。
我可能会尝试这样做:
found=False
if year < 1993: # If less than 93, then month doesn't matter
found = True
elif (year == 1993) and (month <= 4): # If 1993, check month
found = True
if found:
print("You are old enough to drink in Washington")
else:
print("You aren't 21 yet")
您可能会考虑使用date
模块来获取年份和月份,而不是修复该值,以便每个月都有人编辑和更新该程序。
答案 1 :(得分:0)
您将希望使用更多如下所示的代码。您所拥有的月份测试是一个问题,因为它并不是所有测试年份。你的代码最后也不需要elif,因为它只是一个别的。
found=False
if year<1993 or (year==1993 and month<=2):
found =True
print("You are old enough to drink in Washington")
else:
found =True
print("You aren't 21 yet")
答案 2 :(得分:0)
Hmmmm。这是两行代码..
found = (year<1993 or (year==1993 and month<=2)) and True or False
print "You are old enough to drink in Washington" if found else "You aren't 21 yet".