Page 1 of 1
Python Help
Posted: Tue Dec 01, 2009 2:14 am
by MakerOfGames
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?
Code: Select all
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?
Re: Python Help
Posted: Tue Dec 01, 2009 7:54 am
by paradoxnj
Remove all the semi-colons at the end of the lines. Python does not like that.
Re: Python Help
Posted: Tue Dec 01, 2009 5:53 pm
by MakerOfGames
Thanks, I am far to used to Java for my own good. However it still isn't functioning properly.
Code: Select all
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.
Re: Python Help
Posted: Tue Dec 01, 2009 6:35 pm
by Juutis
What version of Python are you using? The method works fine in Python 3.0. The rest of the program doesn't, however...
***EDIT***
Try:
Code: Select all
month=(int)(raw_input("Enter the number of the month you were born in: "))
Re: Python Help
Posted: Tue Dec 01, 2009 6:40 pm
by Allanon
You are inputting the month as a string so you need to convert it to an integer before testing it's value:
Code: Select all
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
Re: Python Help
Posted: Tue Dec 01, 2009 8:26 pm
by MakerOfGames
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.
Re: Python Help
Posted: Tue Dec 01, 2009 9:18 pm
by paradoxnj
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).
This script works.
Code: Select all
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
Re: Python Help
Posted: Tue Dec 01, 2009 9:43 pm
by Allanon
Or you can do this if you don't want to convert the month to an integer:
Code: Select all
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
Re: Python Help
Posted: Thu Dec 03, 2009 3:05 am
by MakerOfGames
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.