Python继续在“while True”中询问raw_input

时间:2015-07-02 06:52:01

标签: python

如果答案不同于1或2,我需要python继续询问raw_input。

这里的例子是:

print """What do you want me to do?

1) Press 1 if you want to .....
2) Press 2 if you want to ....."""

while True:
    answer1 = raw_input(" => ")

    if (answer1 == 1):
        ....
        ....

    elif (answer1 == 2):
        ....
        ....

    elif (answer1 != 1 or 2) or answer1.isalpha():
        print "I need 1 or 2"

问题是如果用户输入1或2,python也会继续询问raw_input。 我哪里错了?

1 个答案:

答案 0 :(得分:0)

如果答案为breakif,则应在elif1块中添加2语句,以打破while循环。< / p>

示例 -

if (answer1 == 1):
    ....
    ....
    break
elif (answer1 == 2):
    ....
    ....
    break