Python Python Basics (2015) Number Game App Number Game Takeaways

Getting 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
PRO
Rick Buffington
Pro Student 8,035 Points

It 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.

Hey 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
PRO
Rick Buffington
Pro Student 8,035 Points

I 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.

Yea 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
PRO
Rick Buffington
Pro Student 8,035 Points

I 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
Robert Peters
2,549 Points

hey, 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!