这是我的两个 def 函数:
def valid_username(username):
# implement the function here
while True:
try:
username = input("Username: ")
if len(username) < 8:
print ("Sorry, the username must be at least 8 characters long.")
if username.isalnum() == False:
print ("Sorry, your name can only contain alpha numeric characters")
numupper = 0
for c in username:
if c.isupper() == True:
numupper += 1
if numupper > 0:
print ("You have at least 1 uppercase in this username.")
else:
print ("You have no uppercase in this username.")
numlower = 0
for d in username:
if d.islower() == True:
numlower +=1
if numlower > 0:
print ("You have at least 1 lowercase in this username.")
else:
print ("You have no lowercase in this username.")
numdigit = 0
for e in username:
if e.isdigit() == True:
numdigit += 1
if numdigit > 0:
print ("You have at least one digit in this username.")
else:
print("You have no digits in this username.")
continue
except:
print ("Sorry, not valid. Try again.")
else:
print ("Thank you for your input")
break
def valid_password(password, username):
# implement the function here
while True:
try:
password = input("Password: ")
if username in password:
print ("That's not secure at all.")
if len(password) < 8:
print ("Sorry, the password must be at least 8 characters long.")
if password.isalnum() == False:
print ("Sorry, your password can only contain alpha numeric characters")
numupper = 0
for c in password:
if c.isupper() == True:
numupper += 1
if numupper > 0:
print ("You have at least 1 uppercase in this password.")
else:
print ("You have no uppercase in this password.")
numlower = 0
for d in password:
if d.islower() == True:
numlower +=1
if numlower > 0:
print ("You have at least 1 lowercase in this password.")
else:
print ("You have no lowercase in this password.")
numdigit = 0
for e in password:
if e.isdigit() == True:
numdigit += 1
if numdigit > 0:
print ("You have at least one digit in this password.")
else:
print("You have no digits in this password.")
continue
except:
print ("Sorry, not valid. Try again.")
else:
print ("Thank you for your input")
break
我的主要程序是这样的:
username = input("Username: ")
result, reason = uservalidation.valid_username(username)
if not(result):
print (reason)
else:
password = input("Password: ")
pwresult, pwreason = uservalidation.valid_password(password, username)
if not(pwresult):
print (pwreason)
else:
print ("Username and Password combination is valid!")
当我运行它时,我得到以下信息:
Username: d
Username: user
Sorry, the username must be at least 8 characters long.
You have no uppercase in this username.
You have at least 1 lowercase in this username.
You have no digits in this username.
Username: craig2345
You have no uppercase in this username.
You have at least 1 lowercase in this username.
You have at least one digit in this username.
Thank you for your input
Traceback (most recent call last):
File "C:/Users/Si Hong/Desktop/pythontest/HuangSiHong_Assign8_part3.py", line 7, in <module>
result, reason = uservalidation.valid_username(username)
TypeError: 'NoneType' object is not iterable
>>>
我无法弄清楚为什么当我输入用户名的第一个值时,它不会触发该功能,但是在我第二次输入它之后它会触发,而且我不知道如何解决 nonetype错误问题,如果有人可以向我解释一下,那就太好了,非常感谢!