Need help ending a while loop

Question:

So I’m having a problem where I can’t figure out how to end a specific while loop.

one = [1, 3, 5, 7, 8, 10, 12]
thirty = [4, 6, 9, 11]

while True:
    try:
        month = int(input("Enter the number of the month: "))
    except month == "":
        print("Program ending")
        break
    except ValueError:
        print("Please enter a number")
        continue
    else:
        def days(month): 
            if month in one:
                return 31
            elif month in thirty:
                return 30
            elif month == 2:
                return 28
        
    if days(month) == None:
            print("The number has to be between 1-12")
    else:
        print("This month has", days(month) ,"days.")

So the program is supposed to tell you how many days are in the specified month and pretty much what I want to achieve is that the loop goes on and asks the question again until the user leaves a blank input (presses enter). I have searched google for quite some time but just can’t seem to find the solution for my exact problem. I found out that you can’t take the value of something in except: if the value was given in try:, which is the case in the code at the moment.
Thanks in advance 🙂

Asked By: Aruand

||

Answers:

A few little mistakes

As Chris said defining a function inside a while loop is of no use
so define the function above you while loop

Secondly you shouldnt try to convert your input to an int before chehing if its ” so you wont get a ValueError

Here is the code fixed

one = [1, 3, 5, 7, 8, 10, 12]
thirty = [4, 6, 9, 11]

def days(month):
    if month in one:
        return 31
    elif month in thirty:
        return 30
    elif month == 2:
        return 28


while True:
    month = input("Enter the number of the month: ")
    if month = '':
        print('program ending')
        break;
    try:
        month = int(month)
    except ValueError:
        print("Please enter a valid number")
        continue
    if days(month) == None:
        print("The number has to be between 1-12")
    else:
        print("This month has ", days(month), " days.")
Answered By: Sheyteo

Some comments regarding your code in general. You defined the input as an integer why would you even try to compare an integer with a string. You could do your string to int conversion a much later. Defining your function in a loop is not a good practice.

Also consider using switch statements or in python dictionaries when you need multiple if statements.

Edit

Catching a ValueError

Here is the adjusted code.

months = {
    1: 31, 2: 28, 3: 31,
    4: 30, 5: 31, 6: 30,
    7: 31, 8: 31, 9: 30,
    10: 31, 11: 30, 12: 31
}

while True:
    month = input("Enter the number of the month: ")
    if month == "":
        print("Program ending")
        break
    try:
        month = int(month)
    except ValueError:
        print("please enter a number")

    if month in months.keys():
        print(months[month])
    else:
        print("not a month")
Answered By: Lost_coder
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.