Nicko Toumbas
Nicko Toumbas

Reputation: 11

How can I get the print_leaderboard() function to work properly and print the top 3 people on the leaderboard?

I am having trouble with getting the names and scores I am appending to scoreboard list to show the top 3 when the print_leaderboard() function is called. Any help would be great!

Here is the code I have.

class ScoreEntry:
    def __init__(self,name,score):
        self.name = name
        self.score = score

    def get_score(score_entry):
        return score_entry.score

class Scoreboard:
    def __init__ (self):
        self.scoreboard = []

    def add_score(self,name,score):
        new_entry = ([name], [score])
        self.scoreboard.append([new_entry])
        return ([new_entry])
    
    def print_leaderboard():
        scoreboard.sort(reverse=True)
        print(scoreboard[0:2])
        
        #Get list of scores, sort them into numerical order by score, then use print to display top three by slicing in descending order
        

# Write your methods here
scoreboard = Scoreboard ()
scoreboard.add_score('Alice', 7821)
scoreboard.add_score('Bob', 12103)
scoreboard.add_score('Charlie', 8762)
scoreboard.add_score('Denise', 6753)
Scoreboard.print_leaderboard ()

That is what I have tried. But I keep getting the error Scoreboard object has no attribute sort What can I do to make this function work. I assume I will need to change some of the other defined functions too.

Upvotes: 0

Views: 89

Answers (1)

Paul M.
Paul M.

Reputation: 10809

There are a few issues:

def add_score(self,name,score):
    new_entry = ([name], [score])
    self.scoreboard.append([new_entry])
    return ([new_entry])

Not sure why you're wrapping all these things in list literals. You're also not using your ScoreEntry class for anything currently. Might as well use it? Also, I don't think this method needs to return anything:

def add_score(self, name, score):
    new_entry = ScoreEntry(name, score)
    self.scoreboard.append(new_entry)

Next:

def print_leaderboard():
    scoreboard.sort(reverse=True)
    print(scoreboard[0:2])

This is not an instance method. Notice how this function takes no parameters (unlike add_score which takes self as the first parameter.) This is a static method (for no good reason?) scoreboard within the scope of this function hasn't been defined, so Python will step out of the current scope and look for that variable in the next more-global scope, which in this case is just the global scope. scoreboard is defined in the global scope, but it's bound to a Scoreboard object, not a list. Lists have a sort method, but Scoreboard objects do not. I think you just want this to be an instance method:

def print_leaderboard(self):
    self.scoreboard.sort(reverse=True, key=lambda entry: entry.get_score())
    print(self.scoreboard[0:2])

The key keyword parameter is required in this case, because self.scoreboard is a list of ScoreEntry objects, and you need to define how they can be sorted/compared.

And then in the calling code: Scoreboard.print_leaderboard() -> scoreboard.print_leaderboard()

Upvotes: 0

Related Questions