为啥我收到错误:AttributeError:'builtin_function_or_method'对象没有属性'isdigit'
Posted
技术标签:
【中文标题】为啥我收到错误:AttributeError:\'builtin_function_or_method\'对象没有属性\'isdigit\'【英文标题】:why i am i getting the error: AttributeError: 'builtin_function_or_method' object has no attribute 'isdigit'为什么我收到错误:AttributeError:'builtin_function_or_method'对象没有属性'isdigit' 【发布时间】:2014-05-11 16:03:52 【问题描述】:我正在创建一个登录系统,在注册时要求用户输入用户名和密码。我使用了一个函数来检查用户名是否有效,然后根据要求查看密码是否有效。(用户名不能已经在使用,必须包含字母)(密码必须包含大写、小写和数)。用户名函数完美运行,但由于某种原因在密码函数中我收到错误: 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'''
【问题讨论】:
只是想知道我做了什么导致这个问题被否决了吗?有没有人对未来的问题有任何建议 发布完整的回溯将是一个好的开始,并解释您迄今为止为解决问题所做的尝试。 感谢您的建议 【参考方案1】:你只崇敬raw_input
,但不要在最后两行调用它。
您的密码检查有问题。所有小写+数字或所有大写+数字都是有效密码。
【讨论】:
在下方输入两次密码进行验证 1234adsf 1234adsf 用户名必须包含大写字母 在下方输入两次密码 AttributeError: 'builtin_function_or_method' object has no attribute 'isdigit' 在下方输入两次密码进行验证 ABCD1224 ABCD1224 用户名必须包含小写字母 在下方输入两次密码 AttributeError: 'builtin_function_or_method' object has no attribute 'isdigit' @JDizzle98 阅读答案的第一行来解决错误,第二行来修复你的逻辑错误。【参考方案2】:当你调用一个函数时,你需要在它周围加上括号()
:
>>> 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 错误,几乎和使用 ==
而不是 =
或其他方式一样常见 :)
【讨论】:
以上是关于为啥我收到错误:AttributeError:'builtin_function_or_method'对象没有属性'isdigit'的主要内容,如果未能解决你的问题,请参考以下文章
为啥我在与 Twilio 交互的 SMS 应用程序中收到错误 AttributeError: 'module' object has no attribute 'Response'?
我收到一个错误:AttributeError:“WebElement”对象没有属性“sendkeys”
为啥我得到 AttributeError:'str' 对象没有属性 'strftime'?
为啥 pytest 在测试模型创建时会抛出“AttributeError:'NoneType'对象没有属性'_meta'”错误?