Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
behar
10,800 PointsHangman game printing??
So im trying to build a simple hangman game.. Im trying to make it so if the user guesses a letter correctly the list_original_word variable will turn the index value where the correct guess is, turn into the guessed letter: So say i have the color blue, it will print X X X X However if i guess blue it will print b X X X Ive attempted this in the code that is highlighted with the comment, but when i run it, it simply prints the entire word. I dont understand this at all, if someone could tell me why, and perhaps come up with a fix it would make my day!
import random
random_words = ["red", "orange", "yellow", "green", "blue", "purple",
"pink", "brown", "grey", "white", "black"]
test = ["red", "blue"]
original_word = random.choice(test)
list_original_word = list(original_word)
correct_guesses = []
wrong_guesses = []
print(len(original_word) * "-")
while True:
guess = input("Guess a letter or a word! ")
if guess in original_word:
print("Yes, {} is in my word!".format(guess))
correct_guesses.append(guess)
# I DONT UNDERSTAND WHY THIS CODE DOES NOT WORK??
index_value = -1
for letter in original_word:
index_value += 1
if guess == letter:
list_original_word[index_value] = guess
print("".join(list_original_word))
else:
print("No, {} is not in my word!".format(guess))
wrong_guesses.append(guess)
1 Answer
Steven Parker
243,199 PointsYour list already contains the letters of the original word, so you don't need to assign your guess to it. But when you print a letter, you'll want to choose between the guess and an "X", plus prevent the newline until all letters have been output.
for letter in original_word:
if guess == letter:
print(guess, end='')
else:
print("X", end='')
print('')
You may also want to revise the game to show all correct guessed letters and not just the most recent one.
I think you might enjoy the Python Basics course, particularly the last project where a hangman game with these exact features is constructed.