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
– Miguel
But that wouldn’t explain why in the original code this error doesn’t happen. Forgive my inexperience, I’m learning to program now!
– Luiz Fernando Andrade
No problem. I tried the original code, and there really isn’t that mistake, I assume it was some minor changes made.
– Miguel
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
– Luiz Fernando Andrade
I deleted your: https://github.com/psnluiz/crispy-sniffle/blob/master/hangman%20game and there was no error
– Miguel
You are declaring
board
up there likeboard = "__" * len (a) == string
and then usingboard [i] = user_choice
. Try to usetype ()
to see if this is it.– Not The Real Hemingway