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
Christopher Gunawan
10,754 PointsHelp with WHILE loop
Hi,
I'm building a Hangman game with Python and I'm a bit stuck with my while loop. Can't seem to figure it out. In this function, I want to create a variable that keeps the number of remaining guesses I have. But for some reason, the remaining guesses never changes even when I increment number of guesses. Please help!
def game_logic(self):
max_guesses = 7
number_of_guesses = 0
remaining_guesses = max_guesses - number_of_guesses
the_word = random.choice(self.the_word_list)
hit_letters = []
missed_letters = []
while remaining_guesses >= max_guesses:
guess = raw_input("Please guess a letter: ")
if guess in the_word:
print("Great, you chose a correct letter. Pick another letter...")
hit_letters.append(guess)
print(self.update_progress(guess, the_word, hit_letters))
elif guess not in the_word:
number_of_guesses += 1
print("Incorrect guess! You have " + str(remaining_guesses) + " guesses remaining. Please try again...")
missed_letters.append(guess)
print(self.update_progress(guess, the_word, hit_letters))
1 Answer
Steven Parker
243,173 PointsThe "remaining_guesses" is calculated before the loop starts on the 3rd line of "game_logic", and then it is never changed.
When you change the value of "number_of_guesses", you would need to calculate "remaining_guesses" again inside the loop to update it.
Christopher Gunawan
10,754 PointsChristopher Gunawan
10,754 PointsThank you for clearing that up!