这是我的两个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
>>>
我无法弄清楚为什么当我输入用户名的第一个值时,它不会触发该功能,但是在我第二次进入该功能后进行,并且我也是不确定如何解决非类型错误问题,如果有人可以向我解释,那将非常感谢你!
答案 0 :(得分:0)
如果函数没有显式返回值,则它会隐式返回None
,这不能应用于以这种方式解包的元组。
答案 1 :(得分:0)
您看到Username:
提示两次的原因是您要求在主程序中输入一次,然后立即再次在函数内部输入。该函数不应该询问用户任何输入,它从传递给它的参数中获取用户名的值。
你看到关于NoneType的错误(不是你写的非类型,Python是区分大小写的,你应该养成完全按照它所说的那样的习惯)的原因是你的函数valid_password()确实完成了返回语句返回程序所期望的两个值。实际上,它根本没有return语句,这意味着它有效地返回特殊的Python值None(这是NoneType类型的对象)。
因为您告诉程序从函数返回的任何内容中提取两个值,所以它试图通过迭代对返回的值进行操作。 NoneType不是可以迭代的类型之一(因为它不包含任何信息),因此是错误消息。