Question

i am trying to create a high scores table, that lists high scores of a game in reverse order, and keeps the top ten scores. i have them defined into 2 functions

ive had 2 problems,

1 - everytime i try and run the display_scores section i get this error:

'function' object is not iterable

2 - when i added multiple scores, each score is replaced instead of being added to the list. for example: Jim with a score of 8 will get replaced when another score is added.

i may be doing something stupid here but im still learning!

my code:

def display_scores():

    print("\n     High Scores\n")
    print("Name", "\t\tScore")
    print("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯")
    for entry in scores:
        name, score = entry
        print(name, "\t\t", score)




def scores():
    """The High Scores Table."""


    scores = []

    name = input("What is your name?")
    score = score_counter
    entry = (name, score)
    scores.append(entry)
    scores.sort(reverse=True)
    scores = scores[:10]

    print("\n     High Scores\n")
    print("Name", "\t\tScore")
    print("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯")
    print(name, "\t\t", score)
Was it helpful?

Solution

you have to 1)change the name of the function and 2) make scores global

scores = []

def display_scores():
    print("\n     High Scores\n")
    print("Name", "\t\tScore")
    print("------------------")
    for entry in scores:
        name, score = entry
        print(name, "\t\t", score)



def scores1():
    """The High Scores Table."""

    global scores 
    name = input("What is your name?")
    score = score_counter
    entry = (name, score)
    scores.append(entry)
    scores.sort(reverse=True)
    scores = scores[:10]

    print("\n     High Scores\n")
    print("Name", "\t\tScore")
    print("--------------")
    print(name, "\t\t", score)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top