Help me fix this code?

A forum for general discussion of the Python programming language.

Help me fix this code?

Postby GABRIELC97 » Tue Jul 08, 2014 6:00 pm

Hello everyone! I'm messing around with this code:

Code: Select all
name = raw_input("Hello, What is your name?")
if len(name) > 0:
    print ("Hello " + name + "! Nice to meet you. I am the God of Awesomeness. However, you can call me Gabriel.")
    play_game = raw_input("Well then, " + name + ", would you like to play a game?")
    if play_game == "Yes" or "yes" or "y":
       
        print("Excellent! Lets continue. We will play...... the conversation game. yes, you will have a conversation with me.")
        print "I will ask questions first"
        color = raw_input("What is your favorite color?")
        print color + ", hmm, interesting. Mine is Green!"
        age = raw_input("How old are you?")
        if age < 18:
            print "well, you are well on your way to the dreadful 'real world'"
        else:
            print "interesting, so you are " +age+ ". Thats great. You gain more life experience with every year!"
        print "Well, that was it! Goodbye!"
    elif play_game == "No" or "no" or "n":
        print("well then, that is quite disappointing. Goodbye, " + name + ".")
    else:
        print ("I do not understand your words of wisdom. Perhaps you shall try an answer like yes or no?")
elif len(name) <=0:
    print ("how about you actually type something?")


anyways, whenever I run the code, except for when I ask for a name, the code accepts any input. For example, if i were to answer 'no' to the question that asks if the user wants to play a game, the code runs the IF statement regardless. However, since my code is running in the first place, I'm assuming that the syntax is correct.

Any help is appreciated!

-Gabriel C.
Last edited by micseydel on Tue Jul 08, 2014 6:48 pm, edited 1 time in total.
Reason: First post lock.
GABRIELC97
 
Posts: 4
Joined: Tue Jul 08, 2014 5:56 pm

Re: Help me fix this code?

Postby micseydel » Tue Jul 08, 2014 7:14 pm

This is a really common and unfortunate stumbling block for new programmers. Python interprets
Code: Select all
if play_game == "Yes" or "yes" or "y":

as
Code: Select all
if (play_game == "Yes") or ("yes") or ("y"):

and not as "if play_game is equal to any of the following". Also, when a string is part of an or it is interpreted as boolean true if it's non-empty, so whether or not play_game == "Yes" is true doesn't matter because "yes" is true. I hope this is clear, if it is not feel free to ask questions and if you don't have any questions I think you can safely not be overly concerned with it for now, it'll click later as your program more.

The proper way to do it is either
Code: Select all
if play_game == "Yes" or play_game == "yes" or play_game == "y":

or more concisely
Code: Select all
if play_game.lower() in ("yes", "y"):

Sometimes people choose to just use str.starswith() to accept things like "yeah" and "ya" although obviously there is the potential for false positives.
Code: Select all
if play_game.lower().startswith('y'):
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1361
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Help me fix this code?

Postby GABRIELC97 » Tue Jul 08, 2014 8:35 pm

Thank you so much! that makes a lot of sense. now i'm curious about this:

If after the user enters an invalid input, i want the question to come up again, would i have to make every question a function?
GABRIELC97
 
Posts: 4
Joined: Tue Jul 08, 2014 5:56 pm

Re: Help me fix this code?

Postby GABRIELC97 » Tue Jul 08, 2014 8:44 pm

Also, Im having the same problem of continuing regardless of input, when i ask for the age. what exactly would i do there?

Code: Select all
 age = raw_input("How old are you?")
       
        if age <= 18:
            print "well, you are well on your way to the dreadful 'real world' so I advise that you study up and prepare yourself."
       
GABRIELC97
 
Posts: 4
Joined: Tue Jul 08, 2014 5:56 pm

Re: Help me fix this code?

Postby micseydel » Tue Jul 08, 2014 9:00 pm

GABRIELC97 wrote:If after the user enters an invalid input, i want the question to come up again, would i have to make every question a function?

Code: Select all
def get_user_input():
    user_in = None
    while not is_valid(user_in):
        user_in = raw_input()
    return user_in

You could even have the is_valid() function be a parameter to get_user_input() if you'll have many different ways to tell when it's valid. You could also not wrap this all into a function, but I definitely recommend you separate out as much as possible the business logic of the code from user interaction. Sanitizing user input before your core code rather than inside of it and then reprompting is definitely preferable.

For age, you're comparing a string to an integer. You need to call int() on the raw_input(), like so
Code: Select all
age = int(raw_input("How old are you? "))
if age <= 18:
    print "lucky!"
else:
    print "that sucks"
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1361
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests