Home > Software engineering >  In python, how would I turn multiple user inputs into a str or a list?
In python, how would I turn multiple user inputs into a str or a list?

Time:03-20

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

CodePudding user response:

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)

CodePudding user response:

guesses = []

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

input #1: a
input #2: b

output: ['a', 'b']

CodePudding user response:

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

input: hi
output:['h', 'i']
  • Related