I am reading Python Crash Course, and the question is given as
9-5. Login Attempts: Add an attribute called
login_attemptsto yourUserclass from Exercise 9-3 (page 166). Write a method calledincrement_ login_attempts()that increments the value oflogin_attemptsby1.Write another method called
reset_login_attempts()that resets the value oflogin_ attemptsto0.Make an instance of the
Userclass and callincrement_login_attempts()several times. Print the value oflogin_attemptsto make sure it was incremented properly, and then callreset_login_attempts(). Printlogin_attemptsagain to make sure it was reset to0.
I defined the class as
class User():
    def __init__(self, first_name, last_name, gender, age):
        """
        User inputs
        """
        self.first_name = first_name
        self.last_name = last_name
        self.gender = gender
        self.age = age
        self.login_attempts = 0
    def describe_user(self):
        """
        Printing the users information
        """
        print("User Name: ", self.first_name)
        print("Last Name: ", self.last_name)
        print("Gender: ", self.gender)
        print("Age: ", self.age)
    def increment_login_attempts(self):
        '''
        Increment the login attempts one by one, each time users try to enter
        '''
        self.login_attempts += 1
        print("You have tried logging in", self.login_attempts, "times")
    def reset_login_attempts(self):
        '''
        Resetting the logging attempts
        '''
        self.login_attempts = 0
        print("Login Attempt has set to ", self.login_attempts)
user = User("Sean", "Dean", "Male", 19)
user.describe_user()
user.increment_login_attempts()
user.increment_login_attempts()
user.increment_login_attempts()
user.increment_login_attempts()
user.reset_login_attempts()
Now my problem is something like this,
When I only write,
user = User("Sean", "Dean", "Male", 19)
user.describe_user()
user.increment_login_attempts()
it prints
User Name:  Sean
Last Name:  Dean
Gender:  Male
Age:  19
You have tried logging in 1 times
and that is fine, but when I close the code and re-run it, it again prints the same thing. So apparently, I am changing the self.login_attempts only in that instance (i.e., when I run the code) but when I close the code and re-run, it goes back to 0
My question is, why is this so ?
I mean its clear that each time I run the program the self.login_attempts is set to 0, but it seems somewhat strange to me.
Is there a way to modify the code such that, in each re-run self.login_attempts will increment by itself, without calling the user.increment_login_attempts() multiple times ? In other words is there a real way of changing the value of the self.login_attempts = 0 , just by re-running the code?
 
    