Python 在名称正确之前,如何对其进行循环?

Python 在名称正确之前,如何对其进行循环?,python,Python,为什么不在你的条件下使用while循环呢。类似于这样,您可以填写以下详细信息: import re username = input("Enter your name to start the test: ") #Prompting the user to input their name valid = re.match("^[A-Za-z]*$",username) #A validation of letters only being used if not valid: pr

为什么不在你的条件下使用while循环呢。类似于这样,您可以填写以下详细信息:

import re
username = input("Enter your name to start the test: ")
#Prompting the user to input their name

valid = re.match("^[A-Za-z]*$",username)
#A validation of letters only being used

if not valid:
    print("Error! Letters only!")
    username = input("Enter your name: ")
试试这个:

username = input("Enter your name to start the test: ")
while not re.match("^[A-Za-z]*$",username):
    print("Error! Letters only!")
    username = input("Enter your name: ")

当您接受空字符串作为有效用户名时,请阅读
的文档?
import re

valid = False

while not valid:
    username = input("Enter your name to start the test: ")
    valid = re.match("^[A-Za-z]*$", username)

    if not valid:
        print("Error! Letters only!")