我有这样的代码:
maybeYes = raw_input("Please enter Yes to continue.")
if maybeYes != "Yes":
print "Try again."
# ask for input again
else:
pass
在我想让它再次要求输入的地方填写什么?
我有这样的代码:
maybeYes = raw_input("Please enter Yes to continue.")
if maybeYes != "Yes":
print "Try again."
# ask for input again
else:
pass
在我想让它再次要求输入的地方填写什么?
您应该直接在循环中执行 raw_input 。
while True:
result = raw_input("...")
if result != "Yes":
print "Try again."
continue
else:
break
这将循环打印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
在我看来你想要一个无限循环。
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