我是一个非常新的程序员。刚开始使用 Python。本质上,我必须编写一个接受用户名输入的程序,并进行一些验证。用户名必须介于 5-10 个字母字符之间。我正在获取代码来测试字符串的长度,但我没有得到它来测试字母字符。我究竟做错了什么?
correct = True
while correct:
username = input('Enter a username that has only alphabetical characters and is between 5 and 10 characters long:')
if username.isalpha:
while len(username) < 5:
print('Invalid username! Please try again.')
username = input('Enter a username that has only alphabetical characters' +
' and is between 5 and 10 characters long:')
if username.isalpha:
while len(username) > 10:
print('Invalid username! Please try again.')
username = input('Enter a username that has only alphabetical characters' +
' and is between 5 and 10 characters long:')
correct = False
else:
print('Username accepted.')