Python 非类型问题和用户名变量问题

Python 非类型问题和用户名变量问题,python,Python,以下是我的两个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 le

以下是我的两个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: 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
>>> 
用户名:d
用户名:user
抱歉,用户名长度必须至少为8个字符。
此用户名中没有大写字母。
此用户名中至少有1个小写字母。
此用户名中没有数字。
用户名:craig2345
此用户名中没有大写字母。
此用户名中至少有1个小写字母。
此用户名中至少有一个数字。
谢谢你的意见
回溯(最近一次呼叫最后一次):
文件“C:/Users/Si Hong/Desktop/pythontest/HuangSiHong\u Assign8\u part3.py”,第7行,在
结果,原因=用户验证。有效的\u用户名(用户名)
TypeError:“非类型”对象不可编辑
>>> 

我很难弄明白为什么当我输入用户名的第一个值时,它不会触发函数,但在我第二次输入后会触发,而且我也不知道如何解决非类型错误问题,如果有人能向我解释一下,那将非常感谢

如果函数没有显式返回值,那么它将隐式返回
None
,这无法应用于元组解包。

出现两次
用户名:
提示的原因是,您在主程序中请求输入一次,然后在函数中立即再次请求输入。函数不应该要求用户输入任何内容,它从传递给它的参数中获取username的值

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


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

您的函数不返回任何内容。
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
>>>