0

这是我的两个 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错误问题,如果有人可以向我解释一下,那就太好了,非常感谢!

4

2 回答 2

0

如果一个函数没有显式返回的值,那么它会隐式返回None,这不能应用于以这种方式解包的元组。

于 2012-12-06T03:44:12.013 回答
0

您看到Username:两次提示的原因是您在主程序中要求输入一次,然后在函数内部立即再次要求输入。该函数不应该向用户询问任何输入,它从传递给它的参数中获取用户名的值。

您看到有关 NoneType 错误的原因(不是您所写的 nonetype,Python 区分大小写,您应该养成准确输入它所说的内容的习惯)是您的函数 valid_password() 确实以 return 语句结束如您的程序所期望的那样返回两个值。事实上,它根本没有 return 语句,这意味着它有效地返回了特殊的 Python 值 None(它是 NoneType 类型的对象)。

因为您告诉您的程序从函数返回的任何内容中提取两个值,所以它试图通过迭代返回的值来做到这一点。NoneType 不是可以迭代的类型之一(因为它不包含任何信息),因此是错误消息。

于 2012-12-06T03:53:52.287 回答