How do I make the code leave the whitespaces on the word
list as whitespaces instead of printing it out as an underscore in the terminal? it displays an underscore at the terminal and doesnt break the code after correctly guessing it which makes the user guess until it breaks by the user failing to guess.
import random
words = ["motherboard", "central processing unit", "power supply", "monitor", "graphics processing unit", "memory", "solid state drive", "mango "]
word = random.choice(words)
guessed_letters = []
incorrect_guesses = 0
max_attempts = 6
while incorrect_guesses < max_attempts:
display_word = ''.join(letter if letter in guessed_letters else '_' for letter in word)
print(f"Word: {display_word}")
if display_word == word:
print("Congratulations! You guessed the word.")
break;
print(f"Incorrect guesses remaining: {max_attempts - incorrect_guesses}")
guess = input("Enter a letter: ").lower()
if len(guess) != 1 or not guess.isalpha():
print("Please enter a single letter.")
continue
if guess in guessed_letters:
print("You've already guessed that letter.")
continue
guessed_letters.append(guess)
if guess not in word:
incorrect_guesses += 1
print(f"Oops! '{guess}' is not in the word.")
if incorrect_guesses == max_attempts:
print(f"Sorry, you ran out of guesses. The word was '{word}'.")
nothing that i know how to do
New contributor
niko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.