1

I'm trying to get the user to put in a specific word.

my code:

import os

os.system("clear")

def get_answer():
    print "\nWould you like to 'hit', 'stick', 'double' down or 'split'?"
    x = raw_input('> ')
    answers = ['hit', 'stick', 'double', 'split']
    y = [i for i in answers if i in x]
    if y == []:
        get_answer()
    print y
    # exit(0)
    return y

def foo():
    a = get_answer()
    print a

foo()

here's my output if I answer 'hit' the first time;

Would you like to 'hit', 'stick', 'double' down or 'split'?
> hit 
['hit']
['hit']

here's my output if I type 'blah' the fist time and then 'hit':

Would you like to 'hit', 'stick', 'double' down or 'split'?
> blah

Would you like to 'hit', 'stick', 'double' down or 'split'?
> hit
['hit']
[]
[]

I don't even really know how to research this. Is it a simple syntax error or is there a deeper issue I just don't understand? I'd love to know how to do this properly.

1
  • the cmd module is probably what you want Commented Aug 25, 2012 at 3:10

2 Answers 2

4

You want to simply test if x in answers, which checks to see if the input is one of the elements in answers.

Also, since you are using recursion to get the user's input, inputting an incorrect value puts another get_answer() call on the stack. The result is that while the innermost get_answer gets valid input, the outer get_answer call(s) continue executing, resulting in the weird output you see.

For example, in your second case, ['hit'] is generated by the innermost call's print y, the first [] is generated by the outer call's print y (since the inner get_answer finishes), and the last [] is generated by print a in foo() (since the outer get_answer returns []).

What you probably want to do instead is either (a) change the get_answer call to return get_answer() so that the innermost call's value is sent back up the stack, or (b) change the get_answer call to a loop, and break out when you get a good answer.

Assuming you're trying to get the user to input exactly one of the options, here's how you could structure the code to use a loop instead of recursion:

def get_answer():
    answers = ['hit', 'stick', 'double', 'split']
    while True:
        print "\nWould you like to 'hit', 'stick', 'double' down or 'split'?"
        answer = raw_input('> ')
        if answer in answers:
            return answer

print get_answer()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much for your detailed answer! However, a code example would be greatly appreciated, as I've tried to employ your suggestion and i'm getting the same problem.
0

The issue is a bit more fundamental. In the get_answer() function, you recurse by calling the function from within itself:

if y == []:
    get_answer()

While this works, I doubt it's your intended behavior. You have to call get_answer() outside of get_answer() in order to prompt for values nicely.

Anyways, here's how I would structure your code:

def get_answer(question, answers):
    response = raw_input(question)

    while response not in answers:
        response = raw_input(question)

    return response

if __name__ == '__main__':
    question = "Would you like to 'hit', 'stick', 'double' down or 'split'?\n> "
    answers = ['hit', 'stick', 'double', 'split']

    print get_answer(question, answers)

2 Comments

Well, not exactly true. If you do 'if y == []: return get_answer()' it actually works fine (unless the user inputs the wrong thing 1000 times and gets a stack overflow...)
If a function reaches the maximum recursion depth, Python just throws a RuntimeError. I don't recommend using recursion in this case because it isn't needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.