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 trialMohammad Zryab
4,109 PointsGetting a never ending loop for the extra credit
I'm attempting the extra credit, where I'm having the computer guess my number. I see a few folks tried it already with different degrees of difficulty - some going as far as coding it with methods that haven't been explained so far.
But in the comp_guess2.py in the workspace here: https://w.trhou.se/yeweqpj9ah
For some reason I'm getting a never ending loop whenever I input a letter as my input. My 'try' and 'except' functions catch the mistake but will replay the ValueError message until workspaces crashes. Any ideas what's going on here?
In the number_game2.py file, I'm following a similar format and it works fine. :)
6 Answers
Rick Buffington
8,146 PointsIt looks like you are getting stuck in the loop because you are never re-prompting the user for input, and comp_guesses never changes value.
Mohammad Zryab
4,109 PointsHey Rick Buffington
Thanks for your response! I see what you're saying, but where would be an appropriate place to re-prompt the user for the input? I tried the below code under the except ValueError but it's not resolving the error.
except ValueError: print("Sorry, but {} isn't a number! :)".format(user_num)) input("Try again, but this time choose a number between 1 and 10") else: # compare my number to the computer number
Rick Buffington
8,146 PointsI would try putting the input statement INSIDE your while loop. That would force it to prompt on each loop iteration. Since it is a while loop, it will run the prompt no matter what.
Mohammad Zryab
4,109 PointsYea I guess that solves the problem - but it is kind of annoying to input it every time. In the original code the computer would keep iterating until it got the right answer. Thanks, though!
Rick Buffington
8,146 PointsI see what you mean. You could try leaving the input where it is before the loop, then putting the input in where the exception is caught. That would force a re-prompt on exception. That makes more sense. Sorry, I obviously wasn't understanding the end goal. I think I am on track now.
Robert Peters
3,728 Pointshey, just noticed but don't know if will affect anything (complete beginner), but in the line: print("Sorry, but {} isn't a number! :)".format(user_num)) there is an extra ')' after the colon, would this not mess with the parenthesis boundaries unless back-slashed? i don't know myself so help would be great!
Robert Peters
3,728 Pointsnvm stupid question!!