-1

我有这样的代码:

maybeYes = raw_input("Please enter Yes to continue.")
    if maybeYes != "Yes":
        print "Try again."
        # ask for input again
    else:
        pass

在我想让它再次要求输入的地方填写什么?

4

3 回答 3

2

您应该直接在循环中执行 raw_input 。

while True:
    result = raw_input("...")
    if result != "Yes":
        print "Try again."
        continue
    else:
        break
于 2012-10-28T22:07:50.773 回答
2

这将循环打印wrong,直到用户键入yes, Yes, YEs, YES,yEs或者yeS在检查之前将输入转换为全大写YES,然后您的代码可以继续...

while raw_input("Please enter Yes to start: ").upper() != 'YES':
       print 'Wrong'

print 'Correct'

#Carry on here 

输出:

Please enter Yes to start: nowg
Wrong
Please enter Yes to start: wggwe
Wrong
Please enter Yes to start: Yes
Correct
于 2012-10-28T22:15:00.633 回答
1

在我看来你想要一个无限循环。

maybeYes = raw_input("Please enter Yes to continue.")
while maybeYes != "Yes":
    maybeYes = raw_input("Please try again.")

但是,您始终可以添加计数器/转义。

maybeYes = raw_input("Please enter Yes to continue.")
attempts = 0
while maybeYes != "Yes" and attempts < 10:
    maybeYes = raw_input("Please try again.")
    attempts += 1
于 2012-10-28T22:09:23.707 回答