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 trialAbdallah El Kabbany
2,042 Pointsnumber game refinement
after finishing the exercise with the teacher everything worked well but it wont stop me after 5 trials it will just keep on going. the teacher didnt test it after modification can somebody help me why the 5 trial limit is not working.
import random
# safely make an int
# limit guesses
# too high message
# too low message
# play again
def game():
# generate a random number between 1 , 10
secret_num = random.randint(1, 10)
guesses = []
while len(guesses) < 5:
try:
# get a number guess from the player.
guess = int(input("guess a number between 1 and 10 : "))
except ValueError:
print("{} isn't a number!".format(guess))
# compare guess to secret.
if guess == secret_num:
# print hit/miss.
print("you got it! my number was {}".format(secret_num))
break
elif guess < secret_num:
print("my guess is higher than {}".format(guess))
else:
print("my guess is lower than {}".format(guess))
else:
print("you didnt get it my number was {}".format(secret_num))
guesses.append(guess)
play_again= input("do you want to play again? Y/n")
if play_again.lower() != "n":
game()
else:
print("bye")
game()
2 Answers
Iain Simmons
Treehouse Moderator 32,305 PointsHi Abdallah El Kabbany, the length of your guesses
list only grows in the else
block where you reveal the answer and then append the guess. The append
should be inside the while
loop (but not inside the if
/elif
/else
blocks), but the answer reveal would be outside the while
loop, since you obviously only want to do that once the loop ends. This means you won't need that else
where you do those two things.
You would also want to check that they ran out of guesses before telling them they didn't get it...
Here's a working version with my suggested changes (and cleaned up formatting):
import random
# safely make an int
# limit guesses
# too high message
# too low message
# play again
def game():
# generate a random number between 1 , 10
secret_num = random.randint(1, 10)
guesses = []
while len(guesses) < 5:
try:
# get a number guess from the player.
guess = int(input("guess a number between 1 and 10 : "))
except ValueError:
print("{} isn't a number!".format(guess))
# compare guess to secret.
if guess == secret_num:
# print hit/miss.
print("you got it! my number was {}".format(secret_num))
break
elif guess < secret_num:
print("my guess is higher than {}".format(guess))
else:
print("my guess is lower than {}".format(guess))
guesses.append(guess)
if len(guesses) == 5:
print("you didn't get it my number was {}".format(secret_num))
play_again = input("do you want to play again? Y/n : ")
if play_again.lower() != "n":
game()
else:
print("bye")
game()
Abdallah El Kabbany
2,042 Pointsthank you :)