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

Python

number game refinement.py

Program works ok. However, once I've guessed correct number and opt to play again, secret_num remains the same value. Looking for confirmation that this is because random.randint occurs outside of loop and second play remains within while loop so subsequent plays won't generate a random number?

Game to guess random number btw 1 -10 in 5 tries

import random 

def game(): 
#Play number game.

  #Generate random number between 1 and 10
  secret_num = random.randint(1, 10)
  guesses = []

  #limit to less than five guesses 
  while len(guesses) < 5:   

      try:
          #Get a int guess from player
          guess = int(input("\nGuess a number bewtween and 1 and 10: ")) 


      except ValueError: 
          print("Blech!!! Enter a whole number btw 1 and 10.")


      else:        
          #Compare guess to secret_num 
          if guess == secret_num: 
              print("You got it!! My number was {}.".format(secret_num))

              #Ask to play again? 
              again = input("\nWould you like to play again? Y/n ") 
              if again.lower() != "n": 
                  guesses = []
                  continue

              else:
                  #End program
                  print("Bye!")
                  break

          elif guess > secret_num:
              #Compare guess to secret_num and indicate too high  
              print("Try again and guess lower than {}.".format(guess))

          else: 
              #Compare guess to secret_num and indicate too low 
              print("Try again and guess higher than {}.".format(guess))

          #capture number of guesses and guess
          guesses.append(guess)
          print("Guess #{} is {}\n".format(len(guesses), guess))

  else:
      #Message of wrong number after 5 attempts 
      print("You didn't get it! My number was {}.".format(secret_num))   

game()    

2 Answers

That's correct.

When you clear guesses you can also refresh secret_num.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Moved to answer. When you clear the guesses you can gen a new secret_num.

Thank you both! Also, what's the protocol for responding to answers... I selected best answer, but not sure if I should do something additional...? Thanks much for helping out newbies.

You are welcome! I'm not sure the protocol since I just recently started Treehouse myself. Happy coding!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

No worries. I purposely responded through a comment so Cole's answer could be marked as best.