如果名称正确,我怎么能有一个循环?

时间:2016-02-03 12:12:51

标签: python

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: ")

2 个答案:

答案 0 :(得分:0)

为什么不在你的病情中使用while循环。像这样的东西,你填写详细信息:

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: ")

答案 1 :(得分:0)

试试这个:

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!")