Home > Software design >  For loop printing all steps, but i only need the last
For loop printing all steps, but i only need the last

Time:02-22

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 do I make it only print what I want? thank you for your time and 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