0

I have written a little code so people can input a date. The error check that stops a month being entered which is less than 1 or greater than 12 should return a value only when it is within these bounds. If I enter a few 'out of bounds' numbers, it correctly re-asks for a month to be re-entered but returns all the values. What is going on?

# the question asked to get the month input for the xml updater
def month_q():
    try:
        month = int(input('What was the month [MM] which the installers were  updated/created by xxx?:'))
    except:
        print("That was not a valid number. Please re-enter a 2 digit month")
        month_q()
    updatemonth = month_check(month)
    print("Month q returning:", updatemonth)
    return updatemonth


# check the update month is a valid month
def month_check(month):
    if month < 1:
        print("The month must be a number between 01 and 12. Please re-enter")
        month_q()
    elif month > 12:
        print("The month must be a number between 01 and 12. Please re-enter")
        month_q()
    else:
        print("Month_check returning month:", month)
        return month



# this updates the xml file with the date of the last installer    
def xml_updater():
    updatemonth = month_q()
    print("Update month:", updatemonth)


xml_updater()

The result is of entering bad months '15', '14' and '13' before entering a correct one '12' is:

What was the month [MM] which the installers were updated/created by xxx?:15
The month must be a number between 01 and 12. Please re-enter
What was the month [MM] which the installers were updated/created by xxx?:14
The month must be a number between 01 and 12. Please re-enter
What was the month [MM] which the installers were updated/created by xxx?:13
The month must be a number between 01 and 12. Please re-enter
What was the month [MM] which the installers were updated/created by xxx?:12
Month_check returning month: 12
Month q returning: 12
Month q returning: None
Month q returning: None
Month q returning: None
Update month: None

What is going on?

2 Answers 2

2

Your function is not returning anything; it is calling the question function again and again, but ignores the return value:

def month_check(month):
    if month < 1:
        print("The month must be a number between 01 and 12. Please re-enter")
        month_q()
    elif month > 12:
        print("The month must be a number between 01 and 12. Please re-enter")
        month_q()
    else:
        print("Month_check returning month:", month)
        return month

If month is smaller than 1 or greater than 12 the function will return None (the default). The statement updatemonth = month_check(month) then assigns None to updatemonth and when print("Month q returning:", updatemonth) runs it prints Month q returning: None. But since you called month_q recursively, that is then returned to the previous month_check() call, which returns None again, and so forth.

Here is a nested trace of your functions, as month_q calls month_check calls month_q, etc.:

month_q():
    What was the month [MM] which the installers were updated/created by xxx?:15
    month_check(15):
        The month must be a number between 01 and 12. Please re-enter
        month_q():
            What was the month [MM] which the installers were updated/created by xxx?:14
            month_check(15):
                The month must be a number between 01 and 12. Please re-enter
                month_q():
                    What was the month [MM] which the installers were updated/created by xxx?:13
                    month_check(13):
                        The month must be a number between 01 and 12. Please re-enter
                        month_q():
                            What was the month [MM] which the installers were updated/created by xxx?:12
                            month_check(12):
                                Month_check returning month: 12
                                <- 12
                            Month q returning: 12
                            <- 12
                        <- None
                    Month q returning: None
                    <- None
                <- None
            Month q returning: None
            <- None
        <- None
    Month q returning: None
    <- None
Update month: None

Instead, have the check function return True or False to indicate correct results, and leave the asking for a re-enter to month_q.

Do that with a while loop instead of recursion though; if you have to ask often enough, you'll end up with a recursion-too-deep exception.

def month_q():
    while True:
        try:
            month = int(input('What was the month [MM] which the installers were  updated/created by xxx?:'))
            if not month_check(month):
                raise ValueError()
        except ValueError:
            print("Please re-enter a 2 digit month")
            continue
        print("Month q returning:", month)
        return month


# check the update month is a valid month
def month_check(month):
    if month < 1 or month > 12:
        print("The month must be a number between 01 and 12")
        return False
    return True

You may as well inline that check:

def month_q():
    while True:
        try:
            month = int(input('What was the month [MM] which the installers were  updated/created by xxx?:'))
            if month < 1 or month > 12:
                raise ValueError()
        except ValueError:
            print("Please re-enter a 2 digit month between 01 and 12.")
            continue
        print("Month q returning:", month)
        return month

Using a blanket except: clause is never a good idea; in the above code I catch the ValueError raised by int() when you enter a non-integer value instead, and raise the same exception if you did enter an integer but it was not a value between 1 and 12 inclusive. That simplifies the 'not a month' error handling significantly.

Sign up to request clarification or add additional context in comments.

1 Comment

May thanks Martijn. That is a nice solution. I appreciated the problem must be the 'print' referring back to the previoulsy returned 'none' values but didn't know how or why it could store them somewhere. I'd mistakenly assumed the final correct value would overwrite the value of month accourdingly.
0

Try this:

try:
    month = int(input('What was the month [MM] which the installers were  updated/created by xxx?:'))
except:
    print("That was not a valid number. Please re-enter a 2 digit month")
    month_q()
    break

That should break stop the printing of incorrect months

Also, you should change

updatemonth = month_check(month)
print("Month q returning:", updatemonth)
return updatemonth

to:

updatemonth = month_check(month)
try:
    updatemonth
except NameError:
    updatemonth = None

if updatemonth is None:
    break
print("Month q returning:", updatemonth)
return updatemonth

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.