首页 > 解决方案 > TypeError 的 Python PWManager 问题:“NoneType”类型的对象没有 len()

问题描述

输入密码后出现以下错误:

添加密码功能后出现此问题。如果我在没有密码功能的情况下运行代码,它将加密并存储

TypeError:“NoneType”类型的对象没有 len()

该程序应该加密密码并存储它。

任何帮助将不胜感激。

谢谢

import re
def encrypt(data, shift):
    encrypted = ""
    for i in range(len(data)):

        char = data[i]
        if (char.isupper()):
            encrypted += chr((ord(char) + shift - 65 % 26 + 65))

        elif (char.islower()):
            encrypted += chr((ord(char) + shift - 97) % 26 + 97)
        elif (char.isdigit()):
            number = (int(char) + shift) % 10
            encrypted += str(number)
        else:
            encrypted += char
    return encrypted





def password_check(password):
    numerics = '0123456789'
    capital_alphabets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    lowercase_alphabet = 'abcdefghigklmnopqrstuvwxyz'
    special_chars = '#?,!'

    sum = 0;
    x = 0;
    y = 0;
    z = 0;
    h = 0
    while sum != 4:
        while len(password) < 10:
            password = input('\nPlease Enter a Correct Password rakam')
        for i in range(len(password)):
            if password[i] in numerics:
                x = 1
            elif password[i] in capital_alphabets:
                y = 1
            elif password[i] in special_chars:
                z = 1
            elif re.search("MIT", password):
                h = 1

        sum = x + y + z + h
        if sum != 4:
            password = input('\nThe Password Must Meet the following CarteriaPlease enter a correct password')
        else:
            print('\nPassword is accepted')



menue = ""

while menue != '1' or menue != '2':
    menue = input("would you like to save a new password or view old ones"
                  "\n1. inputnew password"
                  "\n2. view passwords"
                  "\n3. exit")
    if menue == '1':
        accountname = input("Enter the name of your account")
        password = password_check(input("Enter you Password"))
        shift = 5
        file = open("Savedpasswords.txt", "a")
        file.write(encrypt(accountname,shift)+";|"+encrypt(password,shift)+"\n")
        file.close()

    if menue == '2':
        file = open("Savedpasswords.txt", "r")
        print("accountname\tpassword")
        for i in file:
            data = i.split(";|")
            print(data)

    if menue == '3':
        exit()
Password is accepted
Traceback (most recent call last):
  File "/Users/hamzaabutaleb/PycharmProjects/PasswordManager/Main.py", line 66, in <module>
    file.write(encrypt(accountname,shift)+";|"+encrypt(password,shift)+"\n")
  File "/Users/hamzaabutaleb/PycharmProjects/PasswordManager/Main.py", line 4, in encrypt
    for i in range(len(data)):
TypeError: object of type 'NoneType' has no len()

Process finished with exit code 1

标签: python

解决方案


问题在这里:

password = password_check(input("Enter you Password"))

由于password_check没有显式返回任何内容,因此它隐式返回None,因此这具有设置password为的效果None。要修复它,请将其更改为:

password = input("Enter you Password")
password_check(password)

但是,如果您希望password_check能够提示输入新密码,则应将其更改为返回密码,在这种情况下,您将返回分配 to 的password_check结果password


推荐阅读