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
Bridie Begbie
785 Pointsnumber guess whole script - not working
import random
Line 7 comes up with an error when I run. This is the script from the video. any idea why it isn't working?
random_num = random.randint(1, 10) guessed_nums = [] allowed_guesses = 5
while len(guessed_nums) < allowed_guesses guess = input(“Guess a number between 1 and 10:”)
try:
player_num = int(guess)
except:
print (“That’s not a whole number!”)
break
if not player_num > 0 or not player_num < 11:
print (“That’s too high a number!”)
break
guessed_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(guessed_nums))) break
else: if rand_num > player_num: print (“Nope! My number is higher than {}. Guess #{}.”.format(player_num, len(guessed_nums))
else:
if rand_num < player_num:
print (“Nope! My number is lower than {}. Guess #{}.”.format(player_num, len(guessed_nums)))
continue
if not rand_num in guessed_nums: print (“Sorry! My number was {}.”.format(rand_num))
2 Answers
man odell
1,905 Pointscould u show us all lines ?
man odell
1,905 Pointssorry , I wrote your program with simple changes same as if to elif and so on: import random
random_num = random.randint(1, 10) guessed_nums = [] allowed_guesses = 5
while len(guessed_nums) < allowed_guesses: guess = input("Guess a number between 1 and 10:") try: player_num = int(guess) except: print ("That’s not a whole number!") break if not player_num > 0 or not player_num < 11: print ("That’s too high a number!") break else: if player_num == random_num: print (" You win! My number was {}.".format(rand_num)) print (" It took you {} tries.".format(len(guessed_nums))) break elif random_num > player_num: print ("Nope! My number is higher than {}. Guess {}.".format(player_num, len(guessed_nums))) guessed_nums.append(player_num) continue else: print ("Nope! My number is lower than {}. Guess {}.".format(player_num, len(guessed_nums))) guessed_nums.append(player_num) continue #if not rand_num in guessed_nums: print (“Sorry! My number was {}.”.format(rand_num)) # understand the program then add other codes u need :good luck
Bridie Begbie
785 PointsBridie Begbie
785 PointsI actually just pasted the whole script in to the question but it's been split up with the weird code windows. I guess forum input doesn't like a whole script! If you can read all the text from top to bottom ignoring that some of it has been put into code windows, the whole script is there!