Python Help

Discuss any other topics here
Post Reply
MakerOfGames
Posts: 866
Joined: Fri Jul 08, 2005 4:27 am
Location: PA, USA

Python Help

Post 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?
Think outside the box.
To go on an adventure, one must discard the comforts and safety of the known and trusted.
User avatar
paradoxnj
RF2 Dev Team
Posts: 1328
Joined: Wed Mar 01, 2006 7:37 pm
Location: Brick, NJ
Contact:

Re: Python Help

Post by paradoxnj »

Remove all the semi-colons at the end of the lines. Python does not like that.
Many Bothans died to bring you this signature....
MakerOfGames
Posts: 866
Joined: Fri Jul 08, 2005 4:27 am
Location: PA, USA

Re: Python Help

Post 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.
Think outside the box.
To go on an adventure, one must discard the comforts and safety of the known and trusted.
User avatar
Juutis
Posts: 1511
Joined: Thu Jan 12, 2006 12:46 pm
Location: Finland

Re: Python Help

Post 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:  "))
Last edited by Juutis on Tue Dec 01, 2009 6:43 pm, edited 1 time in total.
Pain is only psychological.
Allanon
Posts: 493
Joined: Mon Aug 29, 2005 8:23 am

Re: Python Help

Post 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
MakerOfGames
Posts: 866
Joined: Fri Jul 08, 2005 4:27 am
Location: PA, USA

Re: Python Help

Post 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.
Think outside the box.
To go on an adventure, one must discard the comforts and safety of the known and trusted.
User avatar
paradoxnj
RF2 Dev Team
Posts: 1328
Joined: Wed Mar 01, 2006 7:37 pm
Location: Brick, NJ
Contact:

Re: Python Help

Post 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
Many Bothans died to bring you this signature....
Allanon
Posts: 493
Joined: Mon Aug 29, 2005 8:23 am

Re: Python Help

Post 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
MakerOfGames
Posts: 866
Joined: Fri Jul 08, 2005 4:27 am
Location: PA, USA

Re: Python Help

Post 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.
Think outside the box.
To go on an adventure, one must discard the comforts and safety of the known and trusted.
Post Reply