Typeerror: 'str' Object does not support item assignment

Asked

Viewed 4,433 times

0

I made my "version" of a game I saw in a book.

def hangman(a):
    stages = ["",
             "________        ",
             "|               ",
             "|        |      ",
             "|        0      ",
             "|       /|\     ",
             "|       / \     ",
             "|               "
    ]

    wrong = 0
    board = "__" * len(a)
    letters = list(a)
    win = False
    print("Welcome to the Hangman game")
    while (wrong < (len(stages) - 1)):
        print ("\n")
        msg = "Guess a letter: "
        user_choice = input(msg)
        if user_choice in letters:
            i = letters.index(user_choice)
            board[i] = user_choice
            letters[i] = '$'

        else:
            wrong += 1
        print((" ".join(board)))
        e = wrong + 1
        print("\n"
              .join(stages[0:e]))
        if "__" not in board:
            win = True
            print ("You won")
            print(" ".join(board))
            break

    if not win:
        print("\n"
              .join(tages[0:wrong]))
        print("It was %s, you lose" %(a))


import random
words = ["yellow", "blue", "black", "green", "gray", "white", "gray", "orange"]
a = random.choice(words)
hangman(a)

However, in the act of running the game I receive or NOT the following error when typing some letter to play:

Traceback (most recent call last):
File "C:/Users/Luiz Fernando/Desktop/PYTHON/magam2.py", line 46, in <module> hangman(a)
File "C:/Users/Luiz Fernando/Desktop/PYTHON/magam2.py", line 22, in hangman
board[i] = user_choice
TypeError: 'str' object does not support item assignment
  • Can you put up the relevant part? But I think it is because you are changing a string 'on the fly' and in python these are immutable: http://stackoverflow.com/questions/10631473/str-object-does-not-support-item-assignment-in-python

  • But that wouldn’t explain why in the original code this error doesn’t happen. Forgive my inexperience, I’m learning to program now!

  • No problem. I tried the original code, and there really isn’t that mistake, I assume it was some minor changes made.

  • Very strange to say the least. I think there is nothing left to do, since my code and the original only diverge on irrelevant things.Thank you for your help

  • I deleted your: https://github.com/psnluiz/crispy-sniffle/blob/master/hangman%20game and there was no error

  • You are declaring board up there like board = "__" * len (a) == string and then using board [i] = user_choice. Try to use type () to see if this is it.

Show 1 more comment

2 answers

1

The problem is that you are modifying a String, which is an immutable object in python.

Try to change the line:

board = "__" * len(a)

for:

board = ["__" for _ in range(len(a))]

or to:

board = ["__"] * len(a)

That way, the line:

board[i] = user_choice

Modifies a list, which is a changeable type.

0

Your variable board is a string of size len(a), who is an immutable guy in Python.

As suggested in the previous answer, you can change this variable so that it is a string array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.