Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8432387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:06:48+00:00 2026-06-10T06:06:48+00:00

I’m trying to get the user to put in a specific word. my code:

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:06:49+00:00Added an answer on June 10, 2026 at 6:06 am

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I need to clean up various Word 'smart' characters in user input, including but
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.