I'm building a game of Hangman as my first python project. The game is currently being run in the terminal as I build it. I'm not using PyGame. Here's more of the relevant code.
five_letters = ['birds', 'hands', 'trees']
difficulty = 'easy'
def get_word(foo):
"""Based on difficulty generate a game_word, then turn it into a list to be returned"""
if foo == 'easy':
foo = random.choice(five_letters)
foo = list(foo)
if foo == 'medium':
foo = random.choice(ten_letters)
foo = list(foo)
if foo == 'hard':
foo = random.choice(fifteen_letters)
foo = list(foo)
return foo
game_word = get_word(difficulty)
I want to take the list and do the following:
- Check if the player's guess is in the list ( I know I can iterate over a string).
- If the guess is in the string place it in the appropriate space in the output.
I'm returning the string as a list so that I can find the value of a correct guess within the list to place it in the correct spot in the output.
For instance
game_word = 'birds'
player_guess = 's'
I want to output
_ _ _ _ s
But maybe I'm going about this all wrong. I just felt like I could shorten the part of the function that selected a random string and then turned it into a list.
foo = list(random.choice(a_list))?random.sample(a_list, 1)would be my choice (it returns a list regardless of the sample size).