thax
thax

Reputation: 23

Error message: object does not contain attribute when object contains attribute

I'm implementing a "player" module with the foliowing:

When running the following change_password method, I receive an error:

while(old_password!=self.password_str):
    AttributeError: 'Player' object has no attribute 'password_str'

Yet, I am defining password_str as attribute of Player. Any ideas what's happening?

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!")

Upvotes: 0

Views: 85

Answers (2)

jimidime
jimidime

Reputation: 552

if password is valid then while loop does not get executed and self.password_str does not get initialized. Try below code:

 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`

Upvotes: 1

Fanir
Fanir

Reputation: 1

If validated returns True for the password passed to __init__, the content of your while loop won't get executed, so the password_str and username attributes will not get set at all.

Upvotes: 0

Related Questions