Home > Back-end >  For loop printing alle steps, but i only need the last
For loop printing alle steps, but i only need the last

Time:02-21

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()

CodePudding user response:

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: 
  • Related