首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的代码一直重复问题提示,而不是继续执行条件语句?

为什么我的代码一直重复问题提示,而不是继续执行条件语句?
EN

Stack Overflow用户
提问于 2021-08-12 05:12:34
回答 2查看 40关注 0票数 0

当我运行我的代码时,即使当输入匹配条件语句时,它也会在while循环开始时继续迭代。当我输入"y“时,它会重复与输入"l”或输入"e“相同的问题提示。

代码语言:javascript
复制
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()
EN

回答 2

Stack Overflow用户

发布于 2021-08-12 05:16:58

您在lower函数中缺少():

代码语言:javascript
复制
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()
票数 1
EN

Stack Overflow用户

发布于 2021-08-12 05:21:02

而不是做:

代码语言:javascript
复制
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()

执行以下操作:

代码语言:javascript
复制
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 = False
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68751894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档