Brandon Brock
Brandon Brock

Reputation: 62

Python - AttributeError: 'str' object has no attribute 'append' - maths game

Trying to make it so a user can add a name, score and what difficulty the user completed the game on to high scores list.

def teacher_page():
    global scores, name, difficulties
    t_choice = None
    while t_choice !="0":
        print("\nWhat would you like to do?")
        print(
        """
        0 - Main Menu
        1 - Add a score
        2 - Remove a score
        3 - View highscores
        """
        )

        t_choice = input("Choice: ")
        print()

    #exit
        if t_choice == "0":
            main_menu()
    #add a score
        elif t_choice == "1":
            names = input("Name of the new user to add?\n")
            name.append(names)
            score = input("What did the user score?\n")
            scores.append(score)
            difficulty = input("And which difficulty did they complete it on?\n")
            difficulties.append(difficulty)
    #remove a score
        elif t_choice == "2":      
            names = input("Name of the user you want to remove?\n")
            if names in name:
                name.remove(names)          
            score = int(input("What did they score?\n"))
            if score in scores:
                scores.remove(score)
    #view highscores
        elif t_choice == "3":
            print("High Scores:")
            for score in scores:
                print(name, score, "on the difficulty ", difficulties)
    #if the t_choice does not = to 0,1,2,3
        else:
            print("Sorry but", t_choice, "isn't a vaild choice.")

But every-time I want to add a user to the list I get the error message

AttributeError: 'str' object has no attribute 'append'

I've had a look at a few examples but not sure where I'm going wrong.

Upvotes: 0

Views: 6915

Answers (1)

Sayan Sil
Sayan Sil

Reputation: 6095

Initialize your variables as lists right below their declaration.

By default, when you assign raw input to them for the first time, they become strings.

Do something like:

global scores, name, difficulties
scores=[]
name=[]
difficulties=[]

During global declaration. Need not initialize again within the function.

Upvotes: 2

Related Questions