0

Struggling to get ValueError to work in python 3 guessing game

I have followed a tutorial to create a guessing game in python 3 and i thought i could make it better so i created a timed intro with error inputs recognition and it all seemed to be going well. However, when i came time to ensure when a player inputted anything other than a number(IE exception handling) it just fails out. The game it self functions just not the non integer in the guessgame I have tried the following with ValueError if, else try, except if guess == ValueError All to no avail, anyway, code is below and in my code area. Any help to solve this would be much appreciated. import time def guessgame(): import random # brings in random generator number = random.randint(1, 20) # assigns random value between 1, 20 numofguesses = 0 # sets number of guesses to 0 print("Hello what is your name?") name = input() print("The number i am thinking of is between 1 and 20", name) # user defined name for numofguesses in range(6):# if number of guesses left is less that 6 do the following print("Take a guess") guess = input() # assign guess variable to user input guess = int(guess) # ensures guess is an number if guess < number: print("Number is too low!") if guess > number: print("Number is too high!") if guess == number: break else: if ValueError: #THIS IS THE JERK THAT WONT WORK print("Please enter a number") if guess == number: numofguesses = str(numofguesses) print("Well done", name, "You guessed the number in: ", numofguesses) gameiniate() if guess != number: number = str(number) print(" Wrong! unlucky, the number was: ", number) def gameiniate(): print("Would you like to play a game? Yes or No") game = input().lower() yeschoice = ("yes", "y") nochoice = ("no", "n")

13th Apr 2019, 9:15 PM
Stuart Powell
Stuart Powell - avatar
5 Respostas
+ 3
try: guess = int(guess) except ValueError: print(...)
13th Apr 2019, 9:32 PM
Anna
Anna - avatar
0
if game in yeschoice: guessgame() elif game in nochoice: # command to exit script if no or n typed print("thats too bad") print("type exit to close") if input() == "exit": exit() else: print("Please respond with Yes or No") gameiniate() time.sleep(4) gameiniate() this is the code missing from the bottom
13th Apr 2019, 9:20 PM
Stuart Powell
Stuart Powell - avatar
0
I have tried it this way: for numofguesses in range(6):# if number of guesses left is less that 6 do the following print("Take a guess") guess = input() # assign guess variable to user input try: guess = int(guess) # ensures guess is an number except ValueError: print("Please enter a number") if guess < number: print("Number is too low!") if guess > number: print("Number is too high!") if guess == number: break and received the following: Traceback (most recent call last): File "C:/Users/LeoStu/.PyCharmCE2019.1/config/scratches/Tutorialscript.py", line 52, in <module> gameiniate() File "C:/Users/LeoStu/.PyCharmCE2019.1/config/scratches/Tutorialscript.py", line 41, in gameiniate guessgame() File "C:/Users/LeoStu/.PyCharmCE2019.1/config/scratches/Tutorialscript.py", line 13, in guessgame guess = int(guess) # ensures guess is an number ValueError: invalid literal for int() with base 10: 'w'
13th Apr 2019, 9:41 PM
Stuart Powell
Stuart Powell - avatar
0
Maybe it's because you're using try/except in a loop https://code.sololearn.com/cer3iUxIy2rE/?ref=app
13th Apr 2019, 10:13 PM
Anna
Anna - avatar
0
Thank you for your help, i have tried it stand alone in the idle and it worked pycharm doesn't appear to run it correctly. although now its not registering going over 6 attempts if a letter is entered until you try a number then works. Thank you again though try_again = True print("Hello what is your name?") name = input() print("The number i am thinking of is between 1 and 20", name) # user defined name while numofguesses < 6:# if number of guesses left is less that 6 do the following while try_again: print("Take a guess") try_again = False guess = input() # assign guess variable to user input try: guess = int(guess) # ensures guess is an number except ValueError: print("Please enter a number") try_again = True numofguesses += 1 numofguesses += 1 if guess < number: print("Number is too low!") try_again = True elif guess > number: print("Number is too high!") try_again = True else: if guess == number: break
13th Apr 2019, 10:43 PM
Stuart Powell
Stuart Powell - avatar