1

I am having difficulty looping my function below. I want my program to evaluate multiple years then terminate the program using an appropriate SL value. Is writing a function the wrong approach? Any help is highly appreciated. Thank you! Code below:

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()
ben
  • 23
  • 3
  • 2
    "Loop it" to what end? The obvious problem is that your `while` condition will stay true indefinitely since you're never modifying the variable `year` inside the loop. – deceze Oct 22 '18 at 00:36
  • Start off with a `while True:` block and break from this when you receive the sentinel value. Also see [this](https://stackoverflow.com/questions/1662161/is-there-a-do-until-in-python) question. I think there is a better answer somewhere on SO but I cant find it. – Paul Rooney Oct 22 '18 at 00:40
  • I would say that writing a function is almost always the best approach. – Jonathan Rys Oct 22 '18 at 00:42

2 Answers2

1

Made a few modifications, seems you wanted to limit input to dates after 1582 so we can make a loop to force that input. After we can increment year if it is not a leap year until it is, and when a leap year is found break.

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!
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
1

I am assuming that by 'evaluating multiple years' , you mean that the program should keep asking for user input until the user explicitly enters a 'sentinel value' to end the program execution. To do that you could simply use a while True loop.

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()
cozek
  • 755
  • 6
  • 9