Ad

For Loop Printing Alle Steps, But I Only Need The Last

- 1 answer

Im making a hangman game in python. and now i got stuck in a problem. i want the for loop to print display that says

wanted output: ['_', '_', '_', '_', '_']

but it prints:
['_']
['_', '_']
['_', '_', '_']
['_', '_', '_', '_']
['_', '_', '_', '_', '_']

i understand this is the for loop, but how to i make it only print what i want? thank you for your time an help!

import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
#Testing code
print(f'Pssst, the solution is {chosen_word}.')
display = []
for letter in chosen_word:
display.append("_")
print(display)
guess = input("Guess a letter: ").lower()
Ad

Answer

Seems like you indented the print(display) statement and added it to the for loop. Just unindent the loop and The code should work.

import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
#Testing code
print(f'Pssst, the solution is {chosen_word}.')
display = []
for letter in chosen_word:
    display.append("_")
print(display)
guess = input("Guess a letter: ").lower()

Output:

Pssst, the solution is aardvark.
['_', '_', '_', '_', '_', '_', '_', '_']
Guess a letter: 
Ad
source: stackoverflow.com
Ad