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:"
checkeroutside the function is a bad idea, and it never returns anything.Noneat the end is printed becausevalid_month("jaNuary")returns None ; not because checker is False. You are printing the return value of function. If you replace it withchecker is Trueyou should see twoNones