0

I am working on a simple hangman game program, so the user inputs a letter as a guess. How would I put these letters into a list?

while not game_over:
  guess = input("Guess a letter. ")
  for pos in range(len(chosen_word)):
    letter = chosen_word[pos]
    if letter == guess:
      display[pos] = letter
  print(display)
  if display.count("_") == 0:
    print("Game over. ")
    game_over = True
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
timelyfor
  • 49
  • 5

3 Answers3

0

You could convert the dict display after the fact, using a for loop:

displayList = []
for i in display.values():
  displayList.append(i)

Using list comprehension:

displayList = [i for i in display.values()]

More info about that: https://www.w3schools.com/python/python_lists_comprehension.asp

Or in the while loop by replacing display[pos] = letter with: displayList.append(letter)

necaris15
  • 11
  • 2
0
guesses = []

while not game_over:
  guess = input("Guess a letter. ")
  guesses.extend(guess)

Given inputs of a and b, this produces a guesses list like ['a', 'b'].

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
timelyfor
  • 49
  • 5
  • Please don't use snippets for languages other than Javascript. Their purpose is to ask the browser to try to run the code, and that doesn't work for other languages. – Karl Knechtel Oct 11 '22 at 03:43
-1

Convert the string to a list using list():

guess = input("Guess a letter. ")
g1 = list(guess)
print(g1)

input: hi
output:['h', 'i']
Prince
  • 71
  • 5