为什么我会收到错误:AttributeError: 'builtin_function_or_method'对象没有属性'isdigit

1 投票
2 回答
4174 浏览
提问于 2025-04-18 06:12

我正在创建一个登录系统,用户在注册时需要输入用户名和密码。我写了一个函数来检查用户名是否有效,然后再检查密码是否符合要求。(用户名不能已经被使用,并且必须包含字母)(密码必须包含大写字母、小写字母和数字)。用户名的检查功能运行得很好,但在密码检查的过程中,我遇到了一个错误:AttributeError: 'builtin_function_or_method' object has no attribute 'isdigit'。有没有人知道我在这两个函数之间做了什么不同的事情,导致一个能正常工作而另一个不行呢?谢谢。

    def Username(user_name):
    user_names = open('Username list.txt', 'r+')
    uname_list = user_names.readlines()
    char_user = [user_name]
    for i in range(len(uname_list)):
        uname_list[i] = uname_list[i].strip('\n')
    for i in range(len(uname_list)):
        if uname_list[i] == user_name:
            return 'username already taken'
    for i in range(len(char_user)):
        if char_user[i].isspace() == True:
            return 'username cannot contain spaces' 
    if user_name.isdigit() == True:
        return 'username must contain letters'
    elif len(user_name) < 4 or len(user_name) > 12:
        return 'username must be between 4 and 12 characters'
    else:
        user_names.write(str(user_name + '\n'))
        file.close(user_names)
        return True

def Password(password, p2):
    passwords = open('Password list.txt', 'r+')
    if password != p2:
        return 'you did not enter the same password twice'
    elif password.isdigit() == True:
        return 'username must contain letters'
    elif password.islower() == True:
        return 'username must contain a capital letter'
    elif password.isupper() == True:
        return 'username must contain a lower case letter' 
    elif password.isalpha() == True:
        return 'username must contain a number'
    elif len(user_name) < 4 or len(user_name) > 12:
        return 'username must be between 4 and 12 characters'
    else:
        passwords.write(str(password + '\n'))
        return True 


print 'What would you like your username to be?'        
print  'Your username must be between 4 and 12 characters, contain letters and not contain any spaces'
user_name = raw_input()
valid = Username(user_name)
while valid != True:
    print valid
    user_name = raw_input()
    valid = Username(user_name)

print 'enter your password twice below for validication'
password = raw_input()
password2 = raw_input()
valid = Password(password,password2)
while valid != True:
    print valid
    print 'enter your password twice below'
    password = raw_input
    password2 = raw_input
    valid = Password(password,password2)

程序运行时发生的事情。

    '''What would you like your username to be?
    Your username must be between 4 and 12 characters, contain letters and not contain any spaces
    Test
    enter your password twice below for validication
    Your password must include capital letters, lowercase letters, numbers and be betweeen 4 and 12 characters
    testing
    testing
    username must contain a capital letter
    enter your password twice below
    AttributeError: 'builtin_function_or_method' object has no attribute 'isdigit'''

2 个回答

1

你只是在提到 raw_input,但在最后两行并没有实际使用它。

你的密码检查有问题。所有小写字母加数字或者所有大写字母加数字都是有效的密码。

1

当你调用一个函数的时候,需要在它的后面加上括号 ()

>>> password = raw_input
>>> password.isdigit() 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'isdigit'
>>> password = raw_input()
67
>>> password.isdigit()
True
>>> 

这是你更新后的代码:

def Username(user_name):
    user_names = open('Username list.txt', 'r+')
    uname_list = user_names.readlines()
    char_user = [user_name]
    for i in range(len(uname_list)):
        uname_list[i] = uname_list[i].strip('\n')
    for i in range(len(uname_list)):
        if uname_list[i] == user_name:
            return 'username already taken'
    for i in range(len(char_user)):
        if char_user[i].isspace() == True:
            return 'username cannot contain spaces' 
    if user_name.isdigit() == True:
        return 'username must contain letters'
    elif len(user_name) < 4 or len(user_name) > 12:
        return 'username must be between 4 and 12 characters'
    else:
        user_names.write(str(user_name + '\n'))
        file.close(user_names)
        return True

def Password(password, p2):
    passwords = open('Password list.txt', 'r+')
    if password != p2:
        return 'you did not enter the same password twice'
    elif password.isdigit() == True:
        return 'username must contain letters'
    elif password.islower() == True:
        return 'username must contain a capital letter'
    elif password.isupper() == True:
        return 'username must contain a lower case letter' 
    elif password.isalpha() == True:
        return 'username must contain a number'
    elif len(user_name) < 4 or len(user_name) > 12:
        return 'username must be between 4 and 12 characters'
    else:
        passwords.write(str(password + '\n'))
        return True 


print 'What would you like your username to be?'        
print  'Your username must be between 4 and 12 characters, contain letters and not contain any spaces'
user_name = raw_input()
valid = Username(user_name)
while valid != True:
    print valid
    user_name = raw_input()
    valid = Username(user_name)

print 'enter your password twice below for validication'
password = raw_input()
password2 = raw_input()
valid = Password(password,password2)
while valid != True:
    print valid
    print 'enter your password twice below'
    password = raw_input()
    password2 = raw_input()
    valid = Password(password,password2)

在你的文件中间,你正确地调用了 raw_input(),但是在最后你忘记加括号了。这是一个简单的Python错误,几乎和把 === 搞混一样常见哦 :)

撰写回答