-2

I am a very new to python and this will probably be easy for you to help me with. So I have a list "guessed" and I want to add the "guess" to it (this is a hangman game by the way), so It works fine for that go in hangman but in the next turn instead of adding another guess to the list it changes the guess.

guessed = []
guessed.extend(guess)
print (guessed)

This is more of my code that you will probably need:

while current != theword and lives > 0:

print ("You have %d lives left" % lives)
guess = input("Please input one letter or type 'exit' to quit.")
guess = guess.lower()


if guess == "exit":
    break


guessed = []
guessed.append(guess)
print (guessed)


if guess in theword:
    index = theword.find(guess)
    x = list(current)
    x[index] = guess
    current = "".join(x)
    print ("Correct!")
    print(x)
    print (guessed)
else:
    print ("Incorrect, try again")

So if I were to guess "m" on the first go it would output "[m]" but then if I guess "a" it would output "[a]" instead of "[m, a]". I think I have to do a loop of something but I cant figure this out.

Thanks

user3160631
  • 11
  • 1
  • 3
  • 2
    Please check and correct the indentation, it is wrong like this. –  Jan 04 '14 at 15:56
  • please fix your indentation. – James Sapam Jan 04 '14 at 16:00
  • Any time you are looping over something and need to keep a running total or keep adding something to a list you have to make sure the variable/list to be changed is declared outside the loop. Other wise you will just keep reinitializing to what ever value you started with. – kylieCatt Jan 04 '14 at 16:20

2 Answers2

1

I assume your indentation is only wrong because of formatting here.

In your while-loop you execute each time guessed = []. This command means "Make a new list and assign it to the name guessed", which effectively overrides the list with the previous guess.

Execute it once before the loop.

Also you use extend in the beginning, but in the main code you use append. Although in this case there is no problem, you might want to read up on the difference: append vs. extend and stick to append in most situations.

Also you do not need to make a list from your string current before changing an element. This is fine, too:

current[index] = guess
Community
  • 1
  • 1
0

Here is how it should be dear!!

current = 's'
theword = 'm'
lives =3
guessed = []
while current != theword and lives > 0:
    print ("You have %d lives left" % lives)
    guess = raw_input("Please input one letter or type 'exit' to quit.\n>>")
    guess = guess.lower()


    if guess == "exit":
        break


    guessed.append(guess)
    print ("You guessed: %s" % " ".join(guessed))


    if guess in theword:
        index = theword.find(guess)
        x = list(current)
        x[index] = guess
        current = "".join(x)
        print ("Correct its '%s'\nYour guesses: %s" % (x, guessed))
    else:
        print ("Incorrect, try again")
Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197
  • Note that this is python 2.x code. For python 3.x one has to change `raw_input` to `input`. –  Jan 04 '14 at 16:06