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

Tim Ott
Tim Ott
2,249 Points

Different ways of creating Python Number game!

This isn't as much a question as me being very curious about how many different ways there are to create this little number game from Python Basics.

I would love to see some other code that was able to create this game besides mine and Kenneth's!

# Number game challenge!
#Players get 5 guesses.
#They must guess a random number.
#It has to be a whole number from 1 to 10
#Tell them if they are too high or too low if they guess wrong
#Tell them how many guesses they have made

import random


random_number = random.randint(1,10)
tries = 0
tries_remaining = 5


while tries < 5:
  guess = input("Guess a random number between 1 and 10. ")
  tries += 1
  tries_remaining -= 1

  try:
    guess_num = int(guess)
  except:
    print("That's not a whole number!")
    break

  if not guess_num > 0 or not guess_num < 11:
    print("That number is not between 1 and 10.")
    break



  elif guess_num == random_number:
    print("Congratulations! You are correct!")
    print("It took you {} tries.".format(tries))
    break


  elif guess_num < random_number:
    if tries_remaining > 0:
      print("I'm sorry, that number is low. You have {} tries remaining.".format(int(tries_remaining)))
      continue
    else:
      print("Sorry, but my number was {}".format(random_number))
      print("You are out of tries. Better luck next time.")



  elif guess_num > random_number:
    if tries_remaining > 0:
      print("I'm sorry, that number is high. You have {} tries remaining.".format(int(tries_remaining)))
      continue
    else:
      print("Sorry, but my number was {}".format(random_number))
      print("You are out of tries. Better luck next time.")

This was the code that I used!

2 Answers

Ricky Catron
Ricky Catron
13,023 Points

This is a variation i created. How good it is I don't know but it demonstrates a large variety of concepts.

import random
import sys

random_number = random.randint(1, 10)
tries = 0
tries_remaining = 5
has_won = False


def test_number(guess_num, tries, tries_remaining, has_won):
    if not guess_num > 0 or not guess_num < 11:
        print("That number is not between 1 and 10.")
        tries -= 1
        tries_remaining += 1

    elif guess_num == random_number:
        print("Congratulations! You are correct!")
        print("It took you {} tries.".format(tries))
        has_won = True

    elif guess_num < random_number:
        if tries_remaining > 0:
            print("I'm sorry, that number is low. You have {} tries remaining.".format(int(tries_remaining)))
        else:
            print("Sorry, but my number was {}".format(random_number))
            print("You are out of tries. Better luck next time.")

    elif guess_num > random_number:
        if tries_remaining > 0:
            print("I'm sorry, that number is high. You have {} tries remaining.".format(int(tries_remaining)))
        else:
            print("Sorry, but my number was {}".format(random_number))
            print("You are out of tries. Better luck next time.")
            sys.exit()

    return (tries, tries_remaining, has_won)


def main(random_number, tries, tries_remaining, has_won):
    while tries < 5:
        guess = input("Guess a random number between 1 and 10. ")
        tries += 1
        tries_remaining -= 1

        try:
            guess_num = int(guess)
            tries, tries_remaining, has_won = test_number(guess_num, tries, tries_remaining, has_won)

        except:
            print("That's not a whole number!")
            tries -= 1
            tries_remaining += 1

        if has_won:
            break

main(random_number, tries, tries_remaining, has_won)
Ingus Mat Burleson
Ingus Mat Burleson
2,517 Points

Here's my shot at it:

import random

guess_counter = 0
low_limit = 0
high_limit = 10
attempts = 0
allowed_attempts = 5
player_win = False


def ask_for_a_guess(low_int, high_int):
    return input("Can you guess my number? It's between {} and {}. ".format(low_int, high_int ))


def guess_legality_checker(guess, low_int, high_int):
    try:
        guess = int(guess)
        if guess > high_int:
            print("I'm sorry, that guess is too high.")
            return False
        if guess < low_int:
            print("I'm sorry, that guess is too low.")
            return False
        print("Hey, that's a good guess...")
        return True
    except:
        print("No, that's either not a number, or it's a fraction.")
        return False


def compare_numbers(target_int, guessed_int):
    if guessed_int < target_int:
        print("But my number is higher!")
        return False
   elif guessed_int > target_int:
        print("But my number is lower!")
        return False
    else:
        print("Wow, you got it!!")
        return True


random_int = random.randint(low_limit, high_limit)

guess = ask_for_a_guess(low_limit, high_limit)

while attempts < allowed_attempts:
    if guess_legality_checker(guess, 0, 10):
        attempts += 1
        if compare_numbers(random_int, int(guess)):
            print("Only took you {} tries!".format(attempts))
            player_win = True
            break
        else:
            guess = input("Try again:")
            continue
    else:
        guess = input("Try again:")
        continue

if False == player_win:
    print("Sorry, you've used all {} turns.  My number was {}".format(allowed_attempts, random_int))