python要求多个输入来导航单个if/elif/els

2024-06-16 09:15:50 发布

您现在位置:Python中文网/ 问答频道 /正文

使用python 2.7.6版

我用python写了一个简单的谜语(来自harrypotter),当命令行中出现提示时,用户必须输入两次“1”,三次“2”,四次“3”(等等),才能得到想要选择的选项。我知道我错过了什么,但不知道是什么。你知道吗

#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()

回答1的结果是:

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

回答7个结果:

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

任何建议都将不胜感激。。。你知道吗


Tags: andtheinreyouinputrawwill
1条回答
网友
1楼 · 发布于 2024-06-16 09:15:50

原因是因为每个if-statement都在调用raw_input(),它在继续之前要求用户输入。您需要存储用户的初始输入,然后继续您的条件

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."

您还应该删除函数中不存在的其他raw_input("> ")

相关问题 更多 >