Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为无效的用户输入循环regex re.match?_Python_Regex_Loops_Input - Fatal编程技术网

Python 如何为无效的用户输入循环regex re.match?

Python 如何为无效的用户输入循环regex re.match?,python,regex,loops,input,Python,Regex,Loops,Input,如果用户输入了无效的内容,我如何重复此操作?请提供一些信息:直到他们提供有效的输入,而不是在最后退出 input_str = raw_input("Please provide some info: ") if not re.match("^[a-z]*$", input_str): print "Error! Only letters a-z allowed!" sys.exit() 您可以重新分配您的输入 将sys.exit替换为input\u str=raw\u inpu

如果用户输入了无效的内容,我如何重复此操作?请提供一些信息:直到他们提供有效的输入,而不是在最后退出

input_str = raw_input("Please provide some info: ")
if not re.match("^[a-z]*$", input_str):
    print "Error! Only letters a-z allowed!"
    sys.exit()

您可以重新分配您的输入

将sys.exit替换为input\u str=raw\u input请提供一些信息:

并将其包装成一个while循环

while not re.match("^[a-z]*$", input_str):
    print "Error! Only letters a-z allowed!"
    input_str = raw_input("Please provide some info: ")
或者,如果您希望保持相同的设置,您可以使用while True并中断

while True:
    input_str = raw_input("Please provide some info: ")
    if not re.match("^[a-z]*$", input_str):
        print "Error! Only letters a-z allowed!"
    else:
        break

试着这样做:

input_str = raw_input("Please provide some info: ")
while not re.match("^[a-z]*$", input_str):
    print "Error! Only letters a-z allowed!"
    input_str = raw_input("Please provide some info: ")

用while not代替if not,用input_str赋值行替换sys.exit你就是刚才帮我问MySQL问题的那个人。再次感谢你,伙计@小猫游行哈哈真棒:很高兴我能帮上忙。如果您还有任何问题,请随时提问:我也会向您抛出a+1: