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
Gary Gibson
5,011 PointsCan't Tally Wins And Losses In This Letter-Guessing Game
I can't get the wins and losses to tally up. The results just come out as "Wins: 0. Losses: 1" or "Wins: 1. Losses: 0" no matter how many games are played.
import random
def game():
the_magic_number = random.randint(1,10)
guesses = []
while len(guesses) < 3:
wins =0
losses = 0
player_guess = (input("Can you guess the right number between 1 and 10 in three tries? "))
try:
player_guess = int(player_guess)
except ValueError:
print("That '{}' is not a number!".format(player_guess))
else:
if player_guess == the_magic_number:
print("You guessed it! {} was the number!".format(player_guess))
wins += 1
print("Wins: {}. Losses: {}.".format(wins, losses))
break
elif player_guess < the_magic_number:
print("{} is too low!".format(player_guess))
elif player_guess > the_magic_number:
print("{} is too high!".format(player_guess))
else:
print("Sorry. That's not right. Try again.")
guesses.append(player_guess)
if len(guesses) < 3:
print("{} guesses left!".format(3 - len(guesses)))
else:
print("You're out of guesses! My secret number was {}.".format(the_magic_number))
else:
print("You didn't get it. My number was {}.".format(the_magic_number))
losses += 1
print("Wins: {}. Losses: {}.".format(wins, losses))
print(wins)
play_again = input("Do you want to play again? Y/n ")
if play_again.lower() == 'n':
print("Okay, see you later!")
print("Wins: {}. Losses: {}.".format(wins, losses))
else:
print("")
game()
game()
Sample results with me losing three times in a row:
Can you guess the right number between 1 and 10 in three tries? 1
1 is too low!
2 guesses left!
Can you guess the right number between 1 and 10 in three tries? 1
1 is too low!
1 guesses left!
Can you guess the right number between 1 and 10 in three tries? 1
1 is too low!
You're out of guesses! My secret number was 3.
You didn't get it. My number was 3.
Wins: 0. Losses: 1.
0
Do you want to play again? Y/n y
Can you guess the right number between 1 and 10 in three tries? 1
1 is too low!
2 guesses left!
Can you guess the right number between 1 and 10 in three tries? 1
1 is too low!
1 guesses left!
Can you guess the right number between 1 and 10 in three tries? 1
1 is too low!
You're out of guesses! My secret number was 10.
You didn't get it. My number was 10.
Wins: 0. Losses: 1.
0
Notice the Wins/Losses results never change past the initial result.
(Note: this is my attempt to alter the game from the lesson.)
2 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsYou are clearing the values of wins and losses inside the game, actually, at every iteration of guessing.
Move initializing theses variables outside of the game function.
Gary Gibson
5,011 PointsI tried that before like this...
import random
wins = 0
losses = 0
def game():
And got the same results.
How should I be doing it?
Gary Gibson
5,011 PointsAh...thank you!
I haven't learned about global declarations yet. I was getting a little ahead of myself and wanted to see if I could figure out how to add this feature with my what I've seen so far in the tracks.
It was clear that I couldn't get the values inside my loop to count outside the loop. Very frustrating.
Thanks again.
Chris Freeman
Treehouse Moderator 68,468 PointsChris Freeman
Treehouse Moderator 68,468 PointsIn addition to moving
winsandlossesoutside of game, since they are assigned inside ofgamethey need to be declared "global" to prevent a local version from being created.Iain Simmons
Treehouse Moderator 32,305 PointsIain Simmons
Treehouse Moderator 32,305 PointsThis would be a good place to use a class, that has a
gamemethod (or better yet, a bunch of smaller functions for each part of the game).Chris Freeman
Treehouse Moderator 68,468 PointsChris Freeman
Treehouse Moderator 68,468 PointsHi Iain, A class would work well... later on. This challenge is from Python Basics. Using a
classis covered in the follow on Object Oriented Python course