0

Im making the hangman game in python 3.11 and I'm trying to make a variable for each character of the word chosen, here is the code:

import os

word = "audio"
    
for i in range(len(word)):
    word[i] = False

while True:
    guessed = ""
    for i in range(len(word)):
        if getattr(sys.modules[__name__], word[i]):
            guessed += word[i]
        else:
            guessed += "_"
    print(guessed)

Though when I try do this i get the error.

    word\[i\] = False
         ^^^
TypeError: 'str' object does not support item assignment.

I have even thought to do this.

for i in range(len(word)):
    eval(f"{word[i]} = False")

What eval does if you don't know it runs the string input as code so it should work but now, I get another error, this is it

    a = False
      ^
SyntaxError: invalid syntax

Which doesn't seem right as that looks like correct syntax?

Anyone have any answers on how I could do this?

It should make a variable with the name being each of character of the chosen word e.g. word = "audio"

a = False
u = False
d = False
i = False
o = False

but have it so that when the word changes the variable names also change in accordance with the word.

PhaseAmaze
  • 13
  • 2

0 Answers0