我是Python的新手,请原谅我的新问题.我有以下代码:
[a while loop starts]
print 'Input the first data as 10 characters from a-f'
input1 = raw_input()
if not re.match("^[a-f]*$", input1):
print "The only valid inputs are 10-character strings containing letters a-f"
break
else:
[the rest of the script]
Run Code Online (Sandbox Code Playgroud)
如果我想,而不是打破循环并退出程序,请将用户返回到原始提示,直到他们输入有效数据,我会写什么而不是中断?
要继续下一个循环迭代,您可以使用该continue语句.
我通常将输入分解为专用函数:
def get_input(prompt):
while True:
s = raw_input(prompt)
if len(s) == 10 and set(s).issubset("abcdef"):
return s
print("The only valid inputs are 10-character "
"strings containing letters a-f.")
Run Code Online (Sandbox Code Playgroud)