Computer scienceProgramming languagesPythonObject-oriented programmingMethods

Methods and attributes

I have friends

Report a typo

Below you can see the class User.

This class has two methods: __init__ and add_friends.

The __init__ method does what any __init__ does, while add_friends increases the number of friends that the user has by the value of n. However, the add_friends method has been defined somewhat incorrectly and right now it doesn't do what it's supposed to do.

Your task is to fix the mistakes in the method.

Pay attention to self.
Write a program in Python 3
class User:
def __init__(self, username):
self.username = username
self.friends = 0

# fix this method
def add_friends(n):
friends += n
print("{} now has {} friends.".format(username, friends))
___

Create a free account to access the full topic