我正在制作一个包含一些验证的程序
number = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
def UsernameValidation():
New_Username_char = ("")
Valid = 0
if not len(New_Username) <= 4:
print("Username too short")
for i in range(0, len(New_Username) - 2):
New_Username_char[i] = str(New_Username[i])
if New_Username_char.islower():
print("Username should be in uppercase")
Valid += 1
if New_Username[len(New_Username)] not in number or New_Username[len(New_Username)-1] not in number:
print("The Username should be followed by two number")
Valid += 1
这些错误出现了
回溯(最后一次调用):文件“C:\Users\Thomas\Desktop\FIle Handling\Mini Project.py”,第 71 行,在 CreateAccount() 文件“C:\Users\Thomas\Desktop\FIle Handling\Mini Project.py”,第 60 行,在 CreateAccount GetUsername() 文件中“C:\Users\Thomas\Desktop\FIle Handling\Mini Project.py”,第 41 行,在 GetUsername UsernameValidation() 文件中“C:\Users\Thomas\ Desktop\FIle Handling\Mini Project.py",第 33 行,如果 New_Username[len(New_Username)] 不在数字中或 New_Username[len(New_Username)-1] 不在数字中:IndexError: string index out of range
当我打印出New_Username
它显示的长度时0
(New_Username 中应该有一些东西)