0
months = ['January',
      'February',
      'March',
      'April',
      'May',
      'June',
      'July',
      'August',
      'September',
      'October',
      'November',
      'December']
checker = False
def valid_month(month):
    for mon in months:
        if mon.lower() == month.lower():
            print mon
            checker = True

    print "out of the loop, checker = " + str(checker)
    if checker is False:
      print None

print valid_month("jaNuary")

This should just print January. But for some reason the output looks like this:

January
out of the loop, checker = True
None

checker is true, then why does it pass the if statement? which is "if checker is False:"

2
  • 1
    Initialising checker outside the function is a bad idea, and it never returns anything. Commented Apr 28, 2016 at 14:25
  • 2
    The None at the end is printed because valid_month("jaNuary") returns None ; not because checker is False. You are printing the return value of function. If you replace it with checker is True you should see two Nones Commented Apr 28, 2016 at 14:28

6 Answers 6

3

You don't return a value from valid_month. At the end of the function, add

def valid_month(month):
    #existing code here

    return checker

You should also bring the checker = False line into your function.

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

Comments

1

Just to note, you can simplify this by using the calendar module to create a mapping of lowercase month name->proper month name, then using that as a lookup for your print, eg:

from calendar import month_name

VALID_MONTHS = {m.lower(): m for m in month_name[1:]}

def valid_month(month):
    return VALID_MONTHS.get(month.lower())

print valid_month('JaNuary')

Comments

1

The whole thing can be reduced to one line and without the need for list comprehensions like in the other answers.

print "jaNuary".capitalize() in months

Comments

0

You don't need all of those loops. Make your valid_month method look like this:

def valid_month(month):
    return month.lower() in [m.lower() for m in months]

if valid_month("jaNuary"):
    print("jaNuary")

Comments

0

As the other answers state, you need to return something from your function in order to print something other than None.

You're printing None currently because valid_month returns None, so the final print statement is printing the return value.

With that said, there's another issue in here:

When you initialize a variable outside of a function in the global scope, you cannot do anything with that variable in of the function.

If you want to modify or access a global variable, you need to start your function with

global var_name

where var_name is the variable name.

Or you could just bring the variable into the function, since it's not needed anywhere else and should be returned from the function, as a general best practice.

Comments

0

You can replace the whole thing with one line. Say month = jaNuaRY

status = "valid" if month.lower() in months else "invalid"

So entries in months should be as you have given above. If you are not sure, then,

status = "valid" if month.lower() in (m.lower() for m in months) else "invalid"

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.