0

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)
ruey694
  • 1
  • 1
  • Aside from the question you actually asked, think carefully about the intended logic of your code. If the value is `>= 90`, will it also be `>= 60`? Which of these conditions is checked first in the code? When that condition is checked, what happens? Given that there is a `return`, will the function continue running? Therefore, will the `>= 90` check occur? – Karl Knechtel Mar 31 '22 at 01:14
  • Thanks for that! I didn't realise the order I was putting my conditions in – ruey694 Mar 31 '22 at 01:22

1 Answers1

0

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()
aaossa
  • 3,763
  • 2
  • 21
  • 34