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 trialyousef alharthi
551 Pointstrying to add "last chance" massage to number game
Hi ! I am trying to write a message to appear after the 4th guessed number to say that you just have one more chance . can anyone help with it ? this is my code
import random
rand_num = random.randint(1,10)
gussed_nums = []
allowed_guesses = 5
last_chance = 4
while len(gussed_nums) < allowed_guesses:
guess = input("guess a number between 1 and 10: ")
try:
player_num = int(guess)
except:
print("thats not a whole number!")
break
if not player_num > 0 or not player_num < 11:
print("that number is not between 1 and 10 ")
break
gussed_nums.append(player_num)
if player_num == rand_num:
print("You win! my number was {}.".format(rand_num))
print("it took you {} tries." .format(len(gussed_nums)))
break
else:
if rand_num > player_num:
print("my number is higher than{}. Guess# {} ".format(player_num, len(gussed_nums)))
else:
if len(gussed_nums) == last_chance :
print("your last chance")
else:
print("nope! my number is lower than {}. Guess# {} ".format(player_num , len(gussed_nums)))
continue
if not rand_num in gussed_nums:
print("sorry ! my number was {}.".format(rand_num))
1 Answer
ducarmont
30,575 Pointsguess = input("guess a number between 1 and 10: ")
I'd maybe put it up above input?
if len(guessed_nums) == allowed_guesses - 1:
print("This is your last guess")
After looking at it some more, it looks like its never getting to that statement.
Try moving it:
if rand_num > player_num:
print("my number is higher than{}. Guess# {} ".format(player_num, len(gussed_nums)))
else:
print("nope! my number is lower than {}. Guess# {} ".format(player_num , len(gussed_nums)))
continue
if len(gussed_nums) == last_chance:
print("your last chance")