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
Whitney Reeds
459 PointsRandom Number Game
I tried writing a script for this game, and nothing I was doing was working quite right. I therefore watched the video with the solution, and copied his code, but for some reason every time I try to open the file, it immediately tells me I have guess wrong.
Here is my code:
import random
rand_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, idiot.")
break
if not player_num > 0 or not player_num < 11:
print("That number isn't between 1 and 10. idiot.")
break
guessed_nums.append(player_num)
if player_num == rand_num:
print("Yay...you're not a total idiot. My number was {}.".format(rand_num))
print("It took you {} tries.".format(len(guess_nums)))
break
else:
if rand_num > player_num:
print("Wrong! My number is higher than {}. Guess #{}".format(player_num, len(guess_nums)))
else:
print("Wrong again! My number is lower than {}. Guess #{}".format(player_num, len(guess_nums)))
continue
if not rand_num in guessed_nums:
print("Sorry, you're an idiot. My number was {}.".format(rand_num))
and this is an example of what is happening:
treehouse:~/workspace$ python guessing_game.py
Sorry, you're an idiot. My number was 8.
treehouse:~/workspace$
What have I done wrong? I've watched the video a couple of times, and I can't figure out what it is that I'm messing up.
2 Answers
Zachary Huntington-Meath
2,221 PointsHi!
So the first part you might want to change is your while loop.
while len(guessed_nums) > allowed_guesses:
You want this statement to be True until you have more guesses than allowed. To do this set the length of the guessed numbers list to being less than the number of allowed guesses.
The second part is you misspelled guessed_nums a couple times later on in your code.
if player_num == rand_num:
print("Yay...you're not a total idiot. My number was {}.".format(rand_num))
print("It took you {} tries.".format(len(guess_nums)))
break
else:
if rand_num > player_num:
print("Wrong! My number is higher than {}. Guess #{}".format(player_num, len(guess_nums)))
else:
print("Wrong again! My number is lower than {}. Guess #{}".format(player_num, len(guess_nums)))
continue
Good luck getting your code working!
Adiv Abramson
6,919 PointsThe statement while len(guessed_nums) > allowed_guesses: is never true, since guess_nums is empty initially and there's no other statement before the while loop that changes that list. So the code immediately after the while loop executes, at the bottom of your script.