Python 3-难度循环功能

时间:2018-10-22 00:30:45

标签: python

我无法在下面循环我的功能。我希望我的程序可以评估多年,然后使用适当的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()

2 个答案:

答案 0 :(得分:1)

进行了一些修改,看来您想将输入限制为1582之后的日期,以便我们可以进行循环以强制输入。之后,如果不是is年,则可以递增year,直到发现a年才休息。

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!

答案 1 :(得分:1)

我假设通过“评估多年”,您的意思是程序应继续要求用户输入,直到用户明确输入“前哨值”以结束程序执行为止。为此,您可以简单地使用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()
相关问题