Reputation: 1
Im new to python and I was wondering if I can get some help. I don't know why my code is not replacing my "_" with the user_answer. Anything helps, thanks.
# Hangman Project
import random
choices = ["Up", "The Hulk", "Iron Man", "Spider man", "Batman"]
answer = list(random.choice(choices))
display = []
display.extend(answer)
for x in range(len(answer)):
display[x] = "_"
print(" ".join(display))
count = len(answer)
guesses = 0
while guesses < len(answer):
user_guess = input("Enter in a letter: ")
user_guess = user_guess.lower
print(guesses)
guesses += 1
for x in range(len(answer)):
if answer[x] == user_guess:
display[x] = user_guess
guesses += 1
print(answer)
Upvotes: 0
Views: 62
Reputation: 38
In addition to @tcdejong's answer, when the user has guessed correctly you are adding the correct guess to the display
list but then not actually displaying it. You could update this section to:
if answer[x].lower() == user_guess.lower():
display[x] = user_guess
print(" ".join(display))
guesses += 1
Upvotes: 1
Reputation: 88
First things first: I recommend you run your code in pythontutor and see some funny things going on that you've probably not intended. For example: The space in "spider man" is also replaced with an underscore.
Additionally, in the setup I recommend replacing this:
display = []
display.extend(answer)
for x in range(len(answer)):
display[x] = "_"
print(" ".join(display))
With this oneliner: display = [c if c == " " else "_" for c in answer]
. Added benefit is that it keeps a space actually a space.
Finally, you're comparing the guess to the a function because you're missing ()
behind .lower
. A letter is never equal to the function because you're not invoking it.
Upvotes: 1