Hello all, I have run into an interesting problem teaching myself python for a college course. It seems I am missing something obvious as I can not get my function to return the proper true or false value. Can someone help me here?
def checkMonth(month):
if month > 12:
return True
elif month < 1:
return True
else:
return False;
print "Python Age Information Program \n";
firstName = raw_input("Enter your first name: ");
lastName = raw_input("Enter your last name: ");
month = 0;
day = 0;
year = 0;
getMonth = True;
while getMonth == True:
month=raw_input("Enter the number of the month you were born in: ");
getMonth = checkMonth(month);
print month;#shows output
print getMonth; #shows output
print checkMonth(month); #shows output
I am a fluent Java programmer and I guess the syntax of Python is really messing with me. My function always returns true and never breaks the while loop, can anyone help me with this?
Think outside the box.
To go on an adventure, one must discard the comforts and safety of the known and trusted.
def checkMonth(month):
if month > 12:
return True
elif month < 1:
return True
else:
return False
print "Python Age Information Program \n"
firstName = raw_input("Enter your first name: ")
lastName = raw_input("Enter your last name: ")
month = 0
day = 0
year = 0
getMonth = True
while getMonth == True:
month=raw_input("Enter the number of the month you were born in: ")
getMonth = checkMonth(month)
print month #shows output
print getMonth #shows output
print checkMonth(month) #shows output
It is supposed to make sure the month value entered by the user is between 1 and 12 but it never registers that the value is correct. Any insight would be appreciated.
Think outside the box.
To go on an adventure, one must discard the comforts and safety of the known and trusted.
def checkMonth(month):
if month > 12:
return True
elif month < 1:
return True
else:
return False;
print "Python Age Information Program \n";
firstName = raw_input("Enter your first name: ");
lastName = raw_input("Enter your last name: ");
month = 0;
day = 0;
year = 0;
getMonth = True;
while getMonth == True:
month=raw_input("Enter the number of the month you were born in: ");
getMonth = checkMonth(int(month));
print month;#shows output
print getMonth; #shows output
print checkMonth(int(month)); #shows output
I am using Python 2.6.4, thanks for the help, that did the trick for converting to int. I didn't realize it wasn't doing that implicitly.
My one professor is requiring everyone to learn a programming language in 2 weeks to give a presentation on it and write a basic program with it. It's a tall order considering I have 4 other classes with homework. It's due at the end of this week, I have been looking over it and trying it myself for the past week. Crazy assignment but I guess that's college. Thanks for the help.
Think outside the box.
To go on an adventure, one must discard the comforts and safety of the known and trusted.
Allanon is correct. You need to cast to an integer. It is also bad practice to use function arguments that have the same name as global variables (month).
def checkMonth(mnth):
if mnth > 12:
return False
elif mnth < 1:
return False
else:
return True
print "Python Age Information Program \n"
firstName = raw_input("Enter your first name: ")
lastName = raw_input("Enter your last name: ")
month = 0
day = 0
year = 0
getMonth = True
while getMonth == True:
month=int(raw_input("Enter the number of the month you were born in: "))
getMonth = checkMonth(month)
print month #shows output
print getMonth #shows output
print checkMonth(month) #shows output
def checkMonth(month):
return (month in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'])
print "Python Age Information Program \n"
firstName = raw_input("Enter your first name: ")
lastName = raw_input("Enter your last name: ")
month = 0
day = 0
year = 0
getMonth = False
while getMonth == False:
month=raw_input("Enter the number of the month you were born in: ")
getMonth = checkMonth(month)
print month #shows output
print getMonth #shows output
print checkMonth(month) #shows output
Thanks for all your help! I got my program working flawlessly now. Also, from trying to learn all of it through personal study with minimal aid(but very thankful for it) I feel that I know Python more than I do C++. And I have had 1.5 semesters of C++ already! Next on my list is to reteach myself C++. But I don't know when that will happen. Thanks again.
Think outside the box.
To go on an adventure, one must discard the comforts and safety of the known and trusted.