Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9157329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:59:19+00:00 2026-06-17T12:59:19+00:00

I have written a little code so people can input a date. The error

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:59:20+00:00Added an answer on June 17, 2026 at 12:59 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a little console application that uses s#arp. I can create an
I have written this little weird piece of code. How is this possible that
can anyone help me? I have some reflection code that i have written and
Our website code is written in PHP. We have very little testing in regards
I am learning flex and have written a little app with a TextArea that
A Little Background I have written a method in javascript/jQuery that loops through fields
I have written this little piece of code: GKPeerPickerController *picker = [[GKPeerPickerController alloc] init];
I have written a little program that parses log files of anywhere between a
I have written some code and used a string that I concatentated using the
I have written down a code that should do garbage collection for c programs.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.