【问题标题】:Python 3 - Difficulty looping functionPython 3 - 难度循环函数
【发布时间】:2019-03-26 00:59:57
【问题描述】:

我在下面循环我的函数时遇到了困难。我希望我的程序评估多年,然后使用适当的 SL 值终止程序。编写函数是错误的方法吗?非常感谢任何帮助。谢谢!代码如下:

def lYr():
    year = int(input("Enter a year: "))


    while year > 1582:

        if int(year) % 4 == 0:
            if int(year) % 100 == 0:
                if int(year) % 400 == 0:
                    print("This is a lp-yr!")
                    break
                else:

                   print("This is not a lp-yr!")
                   break
            else:
                print("This is a lp-yr!")
                break
        else:
            print("This is not a lp-yr!")
            break

    else:
        print("enter correct date")
        break


lYr()

【问题讨论】:

  • “循环”到什么目的?明显的问题是您的 while 条件将无限期保持为真,因为您永远不会在循环内修改变量 year
  • while True: 块开始,并在收到标记值时中断。另请参阅this 问题。我认为在 SO 的某个地方有更好的答案,但我找不到。
  • 我想说写一个函数几乎总是最好的方法。

标签: python


【解决方案1】:

做了一些修改,似乎你想将输入限制在1582 之后的日期,所以我们可以创建一个循环来强制输入。如果不是闰年,我们可以增加year,直到它是闰年,并且当发现闰年时休息。

def leapYear():
    year = int(input("Enter a year after 1582: "))
    while year < 1582:
        year = int(input("Invalid Date. Enter a year after 1582: "))

    while True:
        if int(year) % 4 == 0:
            if int(year) % 100 == 0:
                if int(year) % 400 == 0:
                    print(f"{year} is a leap-year!")
                    break
                else:
                    print(f"{year} is not a leap-year!")
                    year += 1
            else:
                print(f"{year} is a leap-year!")
                break
        else:
            print(f"{year} is not a leap-year!")
            year += 1

leapYear()
Enter a year after 1582: 1900
1900 is not a leap-year!
1901 is not a leap-year!
1902 is not a leap-year!
1903 is not a leap-year!
1904 is a leap-year!

【讨论】:

    【解决方案2】:

    我假设通过“评估多年”,您的意思是程序应该不断询问用户输入,直到用户明确输入“哨兵值”以结束程序执行。为此,您可以简单地使用 while True 循环。

    def leapYear():
    
            #hard code the sentinel value here or declare it outside and pass it
            #to the function
            sentinal_value = 'XYZ'
    
            while True:
                year = input("Enter a year: ")
    
                """We check if the input is the sentinal_value. If so,
                then we break out of the loop"""
    
                if year == sentinal_value:
                    break
    
                """next we check if the input is int, because the user might enter
                garbage"""
                try:
                    year = int(year)
                except:
                    print('Enter value correctly in the format YYYY')
                    continue #we go back to the start of the loop if 
                    #the user enters garbage and ask for input again
    
    
                """next we check if year is < 1582, go back to the start of the while loop
                if that's the case. Else, we run your loop year calculator. """
    
                if year < 1582:
                    print("Enter a value greater than 1582\n")
                    continue
                else:
                    #the breaks are go longer necessary, since we want only the sentinel
                    #value to end the loop.
                    if int(year) % 4 == 0:
                        if int(year) % 100 == 0:
                            if int(year) % 400 == 0:
                                print("This is a leap-year!")
                                #break
                            else:
    
                               print("This is not a leap-year!")
                               #break
                        else:
                            print("This is a leap-year!")
                            #break
                    else:
                        print("This is not a leap-year!")
                        #break
    
                """after evaluating the year, we go back to the start of the while loop"""
    
    
    leapYear()
    

    【讨论】:

      猜你喜欢
      • 2019-03-10
      • 1970-01-01
      • 1970-01-01
      • 2021-02-27
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多