当我运行我的代码时,即使当输入匹配条件语句时,它也会在while循环开始时继续迭代。当我输入"y“时,它会重复与输入"l”或输入"e“相同的问题提示。
intro = "Welcome to the leap year Calculator\n"
print(intro)
leapyear = True
def leap_year_calc():
test= input("What year are you checking? ")
year = int(test)
if ((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0):
prompt = str(year) + " Is a leap year"
print (prompt)
return leapyear == True
else:
prompt = str(year) + " Isn't a leap year"
print (prompt)
return leapyear == True
def leap_year_list():
leap_years = []
for i in range(4001):
if i >= 1752: #this was the 1st leap year in th Gregorian calender
if ((i % 4 == 0) and (i % 100 != 0)) or (i % 400 == 0):
leap_years.append(i)
else:
pass
print(leap_years)
return leap_years
while leapyear != False:
question = input("Do you want to know if a year is a leap year(type 'y'), \nor see a list of leap years(type 'l') or exit (type 'e'? ").lower
if question == 'y':
leap_year_calc()
elif question == 'l':
leap_year_list()
else:
leapyear == False
#leap_year_calc()
#leap_year_list()发布于 2021-08-12 05:16:58
您在lower函数中缺少():
question = input("Do you want to know if a year is a leap year(type 'y'), \nor see a list of leap years(type 'l') or exit (type 'e'? ").lower()发布于 2021-08-12 05:21:02
而不是做:
while leapyear != False:
question = input("Do you want to know if a year is a leap year(type 'y'), \nor see a list of leap years(type 'l') or exit (type 'e'? ").lower
if question == 'y':
leap_year_calc()
elif question == 'l':
leap_year_list()执行以下操作:
while leapyear:
question = input("Do you want to know if a year is a leap year(type 'y'), \nor see a list of leap years(type 'l') or exit (type 'e'? ").lower
if question == 'y':
leap_year_calc()
elif question == 'l':
leap_year_list()
else:
leapyear = Falsehttps://stackoverflow.com/questions/68751894
复制相似问题