0

using python ver 2.7.6

I wrote a simple riddle (from Harry Potter) in python and when the prompt come to the user in the command line, they have to enter "1" twice, "2" thrice, "3" four times (etc.) to get to the option they want to select. I know I'm missing something, but don't know what.

#potter_riddle
print " "
print "Pick Your Poison, Potter"

ascii_bottles = """
#########################################################
#                                          _            #
#                                         ( )     _     #
#     _      ___             _     ___    / \    ( )    #
#    / \    (   )     _     ( )   |   |  |   |   / \    #
#   |   |   /   \    / \    / \   |   |  |   |  |   |   #
#   |___|  (_____)  (___)  (___)  |___|  |___|  |___|   #
#     1       2       3      4      5      6      7     #
#                                                       #
#########################################################
"""

riddle = """
Danger lies before you, while safety lies behind,
Two of us will help you, whichever you would find,
One among us seven will let you move ahead,
Another will transport the drinker back instead,
Two among our number hold only nettle wine,
Three of us are killers, waiting hidden in line.
Choose, unless you wish to stay here for evermore,
To help you in your choice, we give you these clues four:
First, however slyly the poison tries to hide
You will always find some on nettle wine\'s left side;
Second, different are those who stand at either end,
But if you would move onwards, neither is your friend;
Third, as you see clearly, all are different size,
Neither dwarf nor giant holds death in their insides;
Fourth, the second left and the second on the right
Are twins once you taste them, though different at first sight.
"""

def answer():
    if "1" in raw_input():
        print "Hermione\'s advice was terrible! Now you\'re dead and Voldemort wins."
    elif "2" in raw_input():
        print "You\'re a little drunk. Try again."
    elif "3" in raw_input():
        print "Congratulations! Now go find Professor Quirrel."
    elif "4" in raw_input():
        print "Hermione\'s advice was terrible! Now you\'re dead and Voldemort wins."
    elif "5" in raw_input():
        print "Hermione\'s advice was terrible! Now you\'re dead and Voldemort wins."
    elif "6" in raw_input():
        print "You\'re a little drunk. Try again."
    elif "7" in raw_input():
        print "What the hell are you doing back in the Chess Room?"
    else:
        print "No, you cannot use a spell to get out of this. Try a number."
print ascii_bottles
print riddle
print "Which bottle shall you try?" 
raw_input("> ")
answer()

answering 1 results in:

Which bottle shall you try?
> 1
1
Hermione's advice was terrible! Now you're dead and Voldemort wins.

answering 7 results in:

Which bottle shall you try?
> 7
7
7
7
7
7
7
7
What the hell are you doing back in the Chess Room?

Any advice would be much appreciated...

1 Answer 1

1

The reason is because each if-statement is calling raw_input() which asks the user for an input before proceeding. You need to store the user's initial input and then proceed with your conditions

def answer():
    choice = raw_input("> ") # store the input first
    if "1" in choice:
        print "Hermione\'s advice was terrible! Now you\'re dead and Voldemort wins."
    elif "2" in choice:
        print "You\'re a little drunk. Try again."
    elif "3" in choice:
        print "Congratulations! Now go find Professor Quirrel."
    elif "4" in choice:
        print "Hermione\'s advice was terrible! Now you\'re dead and Voldemort wins."
    elif "5" in choice:
        print "Hermione\'s advice was terrible! Now you\'re dead and Voldemort wins."
    elif "6" in choice:
        print "You\'re a little drunk. Try again."
    elif "7" in choice:
        print "What the hell are you doing back in the Chess Room?"
    else:
        print "No, you cannot use a spell to get out of this. Try a number."

You should also remove that other raw_input("> ") which is not in the function

Sign up to request clarification or add additional context in comments.

1 Comment

That makes a lot of sense: I believe what my original code was doing was to ask the user for input within each "if" or "elif" with no "> " prompt in any after the initial "if".

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.