'How can I append values to a parameter list in a class? [duplicate]

The points object is initially an empty list and as the teams participate in different sports their points get added to the list. The scores are then added and averaged, if the average is >= 60, then team places third. If average is >= 80, team places second. If average is >= 90, team places first. The points list should also be in an ascending order.

class Sports:
    def __init__(self, team_name, team_colour, points = []):
        self.team_name, self.team_colour, self.points = team_name, team_colour, points

    def __str__(self):
        average = 0
        try:
            average = sum(self.points)/len(self.points)
            average = average
        except ZeroDivisionError:
            average = 0

        if len(self.points) == 0:
            return self.team_name + " " + str(sorted(self.points)) + " (No participation.)"
        if average >= 60:
            return self.team_name + " " + str(sorted(self.points)) + " (Third)"
        if average >= 80:
            return self.team_name + " " + str(sorted(self.points)) + " (Second)"
        if average >= 90:
            return self.team_name + " " + str(sorted(self.points)) + " (First)"
        else:
             return self.team_name + " " + str(sorted(self.points)) + " (You didn't place. Thanks for participating!)"           
    
    def add_points(self, point):
        if point >= 0 and point <= 100:
            self.points.append(point)



team1 = Sports('Dragons', 'Red')
team2 = Sports('Tigers', 'White')
team3 = Sports('Bulldogs', 'Blue')

team1.add_points(35)
team1.add_points(50)
team1.add_points(10)
team1.add_points(50)

team2.add_points(55)
team2.add_points(75)
team2.add_points(95)
team2.add_points(95)

team3.add_points(89)
team3.add_points(95)
team3.add_points(80)
team3.add_points(98)
print(team1)
print(team2)
print(team3)

I have tried appending it to the points list by:


def add_points(self, point):
        if point >= 0 and point <= 100:
            self.points.append(point)

The expected output should be:

Dragons [10, 35, 50, 50] (You didn't place. Thanks for participating!)
Tigers [55, 75, 95, 95] (Second)
Bulldogs [80, 89, 95, 98] (Fist)

The output I get:

Dragons [10, 35, 50, 50, 55, 75, 80, 89, 95, 95, 95, 98] (Third)
Tigers [10, 35, 50, 50, 55, 75, 80, 89, 95, 95, 95, 98] (Third)
Bulldogs [10, 35, 50, 50, 55, 75, 80, 89, 95, 95, 95, 98] (Third)


Solution 1:[1]

The issue it's well covered in this SO question. The particular problem in your code is that you're using a list as a default argument in your Sport class. Because lists are mutable objects, you're always using the same list, in every function call. Do this instead:

def __init__(self, team_name, team_colour, points=None):
    if points is None:
        points = list()

You could also use the one-liner version:

points = points if points else list()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aaossa