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

Ben Hedgepeth
seal-mask
.a{fill-rule:evenodd;}techdegree
Ben Hedgepeth
Python Web Development Techdegree Student 10,287 Points

Code critique - Computer Guessing Number Game

Please provide suggestions/improvements anywhere in my code. Thanks!

Objective: Have the computer guess my number under 5 attempts.

def you_guess():
  import random

  secret_number = 7
  guess_limit = 5

  def machine_guess():
    guess = random.randint(1, 10)
    return guess

  def guidelines():
    print("Welcome Mr.Computer! Now it's your turn to guess my secret number."
          '''
          Here are some guidelines:
          It's a number between 1 and 10
          You have five guesses to get it right
          ''')

  guidelines()

  while guess_limit > 0:
    attempt = machine_guess()
    guess_limit -= 1

    if attempt > secret_number:
      print("You guessed {0}. That's too high! {1} trys left!".format(attempt, guess_limit))
    elif attempt < secret_number:
      print("You guessed {0}. That's too low! {1} trys left!".format(attempt, guess_limit))
    else:
      print("You got it right on guess attempt #{}!".format(5 - guess_limit))
      break


you_guess()