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 Python Basics (Retired) Pick a Number! Any Number! The Solution

Y. Kravets
Y. Kravets
10,350 Points

Proposed solution to the game for comments and improvements (haven't seen the real one yet)

Hi guys!

So I decided to try and make the game work before looking at the actual solution. This is what I have written. Seems to work reasonably well, but I'd like to have an opinion from Python gurus here if there are ways of further improvement of this code. Thanks in advance!

P.S. I will look at the real solution in the video now.

import random

def helper(a,b,guesses):
  print ('Welcome to the number guessing game written in Python!')
  print ('You will have to guess an integer number within a range [{},{}] with {} guesses'.format(a,b,guesses))

def congrats(number, guesses_counter):
  print ('Congratulations! {} is the correct number'.format(number))
  print ('It took you {} guesses'.format(guesses_counter))

def greater(number, guesses_counter):
  print ('Number {} you entered is greater than the target number. Try again!'.format(number))
  print ('You have used {} guesses'.format(guesses_counter))

def smaller(number, guesses_counter):
  print ('Number {} you entered is smaller than the target number. Try again!'.format(number))
  print ('You have used {} guesses'.format(guesses_counter))

lower_limit = input('>> Provide a lower limit for the number range: ')
higher_limit = input('>> Provide a higher limit for the number range: ')
guesses_limit = input('>> Provide the number of guesses (has to be greater than 0) you want to limit yourself with: ')

a = int(lower_limit)
b = int(higher_limit)
guesses = int(guesses_limit)
guesses_counter = 0

target = random.randint(a,b)

helper(a,b,guesses)

while True:
  number_str = input('>> Try and guess the number between {} and {}: '.format(a,b))
  number = int(number_str)

  if number == target and guesses_counter < guesses:
    guesses_counter = guesses_counter + 1
    congrats(number,guesses_counter)
    break
  elif number > target and guesses_counter < guesses:
    guesses_counter = guesses_counter + 1
    greater(number,guesses_counter)
    continue
  elif number < target and guesses_counter < guesses:
    guesses_counter = guesses_counter + 1
    smaller(number,guesses_counter)
    continue
  elif guesses_counter >= guesses:
    print('You have used all available guesses! Please restart the game!')
    break
Y. Kravets
Y. Kravets
10,350 Points

Sorry for the way the code above is formatted. When I am trying to edit the post the formatting is fine, but as soon as I save it it messes up!

EDIT: OK, managed to correct the code formatting so its readable now

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Hey, not a bad try at all! Couple of things you can make a bit tighter but I bet you picked those up from the video.

Keep up the good work!