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 trialSean Flanagan
33,235 PointsMy refinement of number_game.py
Hi everyone.
I've just tweaked this program, as per Kenneth's suggestion. I've made some modifications:
- The player has to guess a number between 1 and 100.
- If the guess is correct, the player will be congratulated.
- If the guess is too low, the player will be told to guess higher.
- 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 :-)