Herrmens,

I think the other guys have already explained it quite well, but here are some more goodies that might interest you.

Your first attempt of filling the display is the more pythonic way in my opinion and it works, so instead of initializing an empty array and the filling it, just use display = [“_”]*word_length

Also for evaluating if the guess is in the word, there is a very nice iterator called enumerate, that hands you two values, the index and the actual value of the item, so you can use it like this:


<span style="color:#323232;">for position, letter in enumerate(chosen_word):
</span><span style="color:#323232;">    if letter == guess:
</span><span style="color:#323232;">        display[position] = letter
</span>

Also, to play the full game you want to surround your guessing part with a while loop, so you can keep guessing until you have found the word. For this you will have to create a list of characters that resemble your chosen_word. There are several ways to do so and I will try to explain some of them.

Here we are using the unpacking asterisk, that unpacks each character of your string into an item in the list


<span style="color:#323232;">while (display != [*chosen_word]):
</span><span style="color:#323232;">    guess = input("Guess a letter: ").lower()
</span><span style="color:#323232;">
</span><span style="color:#323232;">    for position, letter in enumerate(chosen_word):
</span><span style="color:#323232;">        if letter == guess:
</span><span style="color:#323232;">            display[position] = letter
</span><span style="color:#323232;">    print(display)
</span>

Another way would be explicitly casting the string into a list with the list function like this:


<span style="color:#323232;">while (display != list(chosen_word)):
</span>

Last but not least we could use something like list comprehension, which is seen as very pythonic but a bit weird to look at when you are not used to it.


<span style="color:#323232;">while (display != [letter for letter in chosen_word]):
</span>

What this essentially does is the same as creating a for loop and filling a list like this, but more comprehensive:


<span style="color:#323232;">chosen_word_list = []
</span><span style="color:#323232;">for letter in chosen_word:
</span><span style="color:#323232;">    chosen_word_list.append(letter)
</span>

W3Schools has some nice info about list comprehension. It is a rather advanced concept though so don’t let it bother you if you don’t get it right away.

Happy coding :)

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • uselessserver093
  • Food
  • aaaaaaacccccccce
  • [email protected]
  • test
  • CafeMeta
  • testmag
  • MUD
  • RhythmGameZone
  • RSS
  • dabs
  • Socialism
  • KbinCafe
  • TheResearchGuardian
  • Ask_kbincafe
  • oklahoma
  • feritale
  • SuperSentai
  • KamenRider
  • All magazines