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
Blair Murphy
12,367 PointsPython basics module - the challenge/solution
Hi - I'm having difficulty in coming up with the solution for the number_guess game. Is there a pattern I can follow that will help with working through each step when tasked with not only this project but all projects moving forward?
When I follow the challenge guidelines:
Make a game that...
- Picks a random number between 1 and 10 and asks the user to guess a number.
- If the guess is wrong, tell the user if the guess is too high or too low, and let the user guess again.
- If it is the right number, congratulate them.
Extra Credit
- Limit the number of guesses
- Tell the user how many guesses that the user has made or both.
...I get stopped up rather quickly.
In the solution video, Kenneth states that imports should generally be at the top (best practice). That part I had no issue with, lol. From there things get murky and I'm not sure how people seem to pull the next steps on the fly.
This is where I wish (maybe there is and I'm not noticing it yet?) there was a pattern to follow something like:
- import
- .randint()
- (in this case - "guesses")
- loop - while
- input
- try <-- I was really thrown by this by the way ...and so on, and so on.
I finished the modules and have the green light for the next module "collections", but I'm hesitant to start it because I don't think I could honestly complete THIS project yet on my own.
Is it ok to move on without a full understanding? I don't want to complete all the modules for the sake of completing it, I really want to be able to someday get to a level where I could, for example, teach my son Python.
Thanks in advance for any help.
P.S. Here's the code for the solution:
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!")
break
if not player_num > 0 or not player_num < 11:
print("That number isn't between 1 and 10!")
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:
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))
*Sorry, this copied strangely from the workspaces number_guess.py file
[MOD: added ```python code and other formatting. -cf]
1 Answer
Josh Keenan
20,315 PointsHey Blair, programming can be a bit weird at times can't it? You understand everything you're doing and then BAM! You don't fully understand what's in front of you, that's alright and it happens to everyone at somepoint and all that matters is how you deal with it. Personally, I try to keep on going for a bit, and then if I'm still not understanding something I go back to where I was before I got confused. I would say go on a bit further and come back to the game after you have started on collections, you'd be surprised how much simpler it will seem with a little bit more experience! If you don't understand what you're doing in collections, head back to where it started getting a little hazy and try it again. Don't use your old code, re-write it all your own way and then compare your new code to your old code, spot differences and see if you now can see how one piece of code is better than another. Good luck, and remember to use the forums if you need any help!
Blair Murphy
12,367 PointsBlair Murphy
12,367 PointsThanks Josh! I'll keep pushing along!