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 (2015) Number Game App Number Game Introduction

Sean Flanagan
Sean Flanagan
33,235 Points

My refinement of number_game.py

Hi everyone.

I've just tweaked this program, as per Kenneth's suggestion. I've made some modifications:

  1. The player has to guess a number between 1 and 100.
  2. If the guess is correct, the player will be congratulated.
  3. If the guess is too low, the player will be told to guess higher.
  4. If the guess is too high, the player will be told to guess lower.

Here it is:

import random

# generate random number between 1 and 100
secret_num = random.randint(1, 100)

while True:
    # get number guess from player
    guess = int(input("Guess a number between 1 and 100: "))

    # compare guess to secret number
    if guess == secret_num:
        print("Well done! My number was: {}".format(secret_num))
        break
    elif guess < secret_num:
        print("Higher!")
    else:
        print("Lower!")

Have fun! I did!

Sean :-)