[Python] in a class, create create a method that adds a number to an attribute

Discussão em 'Python' iniciado por Stack, Agosto 27, 2025.

  1. Stack

    Stack Membro Participativo

    I wrote a class to describe a user's information and created an attribute of the number of login attempts. When I added the method to increment the login attempts, I receive an error "int object is not callable." How would we try to create an method that increments by # of attempts?

    class User:
    def __init__ (self, first_name, last_name, age, location):
    """Initialize attributes of user."""
    self.first_name = first_name
    self.last_name = last_name
    self.age = age
    self.location = location
    self.login_attempts = 0

    def describe_user(self):
    """Print user's info."""
    print(f"User's info: {self.first_name.title()} {self.last_name.title()} is {self.age} and lives in {self.location.title()}.")

    def greet_user(self):
    """Greet the user."""
    print(f"{self.first_name.title()}, welcome!")

    def increment_login_attempts(self, attempts):
    """Add the number of login attempts."""
    self.login_attempts += attempts

    def reset_login_attempts(self, rest):
    """Reset the number of login attempts."""
    self.login_attemps = 0 ```

    user_1 = User("bob", "smith", 18, "malibu")

    user_1.describe_user()
    user_1.greet_user()
    print(user_1.login_attempts)
    user_1.increment_login_attempts(10)

    TypeError Traceback (most recent call last)
    Cell In[34], line 6
    4 user_1.greet_user()
    5 print(user_1.login_attempts)
    ----> 6 user_1.increment_login_attempts(10)

    Cell In[25], line 22, in User.increment_login_attempts(self, attempts)
    20 """Add the number of login attempts."""
    21 self.login_attempts += attempts
    ---> 22 print(f"The number of login attempts is {self.login_attempts()}")

    TypeError: 'int' object is not callable ```

    Continue reading...

Compartilhe esta Página