我正在实施一个带有下载的“播放器”模块:
运行以下change_password
方法时,收到错误:
while(old_password!=self.password_str):
AttributeError: 'Player' object has no attribute 'password_str'
然而,我将password_str
定义为Player
的属性。任何想法发生了什么?
class Player(object):
def __init__(self, username_str="-", password_str=""):
while (not validated(password_str)):
self.password_str=input("Password is invalid. Please try again:")
self.username=username_str
def change_password(self,old_password):
MAX_ATTEMPT=3
num_of_fails=0
while(old_password!=self.password_str):
num_of_fails+=1
if(num_of_fails<MAX_ATTEMPT):
old_password=input("Thej passwword entered is invalid. Please try again. \
(You have "+str(MAX_ATTEMPT-num_of_fails)+" attempts reamining)")
else:
print("Incorrect password entered too many times. Your account is temporarily locked.")
break
if(num_of_fails<MAX_ATTEMPT):
new_password=input("please enter a new password.")
while(not validated(new_password)):
new_password=input("New password is invalid. Please try again")
self.password_str=new_password
print("Password has been successfully changed!")
答案 0 :(得分:1)
如果密码有效,则while循环未执行且self.password_str未初始化。请尝试以下代码:
def __init__(self, username_str="-", password_str=""):
self.password_str = password_str
while (not validated(self.password_str)):
self.password_str=input("Password is invalid. Please try again:")
self.username=`enter code here`
答案 1 :(得分:0)
如果传递给validated
的密码的__init__
返回True,则您的while循环内容将无法执行,因此password_str
和username
属性根本不会设置。