Run statement in terminal if user enters a certain key otherwise continue with raw_input in python
Question: I would like to be able to have the terminal ask questions to the user but I
also want the user to be able to enter an alternate character which would be
able to print something out and I was wondering how to do this throughout
several user inputs. Thanks this is a chunk of my code.

    
    
    user_choice = []
    if user_choice == "b":
        print "we have: strawberry, chocolate, vanilla, and mint."
    
    else:
        # run the code like normal 
    
    user_choice.append( raw_input("would you like some ice cream?: "))
    if user_choice[-1] =="yes":
        print "Here you go."
    user_choice.append( raw_input("would you like some cake?: "))
    if user_choice[-1] =="yes":
        print "Ooh it looks like we just ran out of cake."
    


Answer: Personally, I would make a function that computes your responses based on the
current question and the user input. You can also have a separate function
which handles the convoluted process of asking questions so you don't need to
repeat your code much. It makes it easy to extend your code this way.

    
    
    import sys
    
    def getResponse(question, userInput):
        # User giving input that doesn't answer the question, but still 
        # invokes an appropriate response
        if userInput == 'b': 
            return ("We have: strawberry, chocolate, vanilla, and mint.", False)
        # Finally, if the user gives input that answers your question
        # default case for unhandled input is included
        else:
            return {
                ("would you like some ice cream?: ", "yes") : 
                    ("Here you go.", True),
                ("would you like some cake?: ", "yes") :
                    ("Ooh it looks like we just ran out of cake.", True)
            }.setdefault((question, userInput), ("", False))
    
    def askQuestion(question):
        sys.stdout.write(question)
        answered = False
        while not answered:
            response, answered = getResponse(question, raw_input())
            if response: print response
    
    # Ask questions
    askQuestion("would you like some ice cream?: ")
    askQuestion("would you like some cake?: ")
    

The functionality you ask for can be easily handled through the use of flow
control based on conditionals. Notice how I only used an if statement and a
while loop to find the solution. I also used an anonymous dictionary, but this
is not necessary, another if statement could have easily done the trick. if
statements and while loops are examples of flow control.

You can find more information on flow control in Chapter 3 of the Python
tutorial.

