我正在创建一个登录系统,在注册时要求用户输入用户名和密码。我使用了一个函数来检查用户名是否有效,然后根据要求查看密码是否有效。(用户名尚未使用且必须包含字母)(密码必须包含大写字母,小写字母和号)。用户名功能完美,但由于某些原因,我在密码功能中收到错误:AttributeError:' builtin_function_or_method'对象没有属性' 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'''
答案 0 :(得分:1)
你只尊敬raw_input
但不要在最后两行中调用它。
您的密码检查有问题。所有低位+数字或全部高位+数字都是有效密码。
答案 1 :(得分: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错误,几乎与使用==
而不是=
或其他方式一样常见:)