ShaunaTheDead, (edited )
@ShaunaTheDead@kbin.social avatar

This is a pretty compact and - I think - easy to read way of doing it:

while(display != list(chosen_word)):

guess = input("Guess a letter: ").lower()

display = list(map(lambda c, d: c if d != '_' or c == guess else d, chosen_word, display))

print(display)

print("Congrats! You did it!")

Mapping over an array is a pretty powerful tool and also using ternary expressions. If you're not familiar, a map basically just iterates over an array and runs a function on that item, replacing it with whatever the return value of the function is.

For example:

ones = [1, 1] twos = list(map(lambda n: n + 1, ones))

It's running the lambda function with n as a parameter and returning n + 1, and it's pulling the numbers from the array "ones".

Then ternary expressions I also find quite powerful. The format of which is basically:

(result if true) if (condition to check) else (result of false)

Or:

2 if 1 + 1 == 2 else "You broke math. How did you do that?"

  • 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