ValueError: invalid literal for int () with base 10: 'stop'

Every time I try to execute the code, it works, but when I type 'stop' , it gives me an error:

ValueError: invalid literal for int () with base 10: "stop"

 def guessingGame(): global randomNum guessTry = 3 while True: guess = input('Guess a Number between 1 - 10, You have 3 Tries, or Enter Stop: ') if int(guess) == randomNum: print('Correct') break if int(guess) < randomNum: print('Too Low') guessTry = guessTry - 1 print('You have, ' + str(guessTry) + ' Guesses Left') if int(guess) > randomNum: print('Too High') guessTry = guessTry - 1 print('You have, ' + str(guessTry) + ' Guesses Left') if guessTry == 0: print('You have no more tries') return if str(guess) == 'stop' or str(guess) == 'Stop': break 
+4
source share
3 answers

The string passed to int() should contain only numbers:

 >>> int("stop") Traceback (most recent call last): File "<ipython-input-114-e5503af2dc1c>", line 1, in <module> int("stop") ValueError: invalid literal for int() with base 10: 'stop' 

A quick fix will be to use exception handling :

 def guessingGame(): global randomNum global userScore guessTry = 3 while True: guess = input('Guess a Number between 1 - 10, You have 3 Tries, or Enter Stop: ') try: if int(guess) == randomNum: print('Correct') break if int(guess) < randomNum: print('Too Low') guessTry = guessTry - 1 print('You have, ' + str(guessTry) + ' Guesses Left') if int(guess) > randomNum: print('Too High') guessTry = guessTry - 1 print('You have, ' + str(guessTry) + ' Guesses Left') if guessTry == 0: print('You have no more tries') return except ValueError: #no need of str() here if guess.lower() == 'stop': break guessingGame() 

And you can use guess.lower() == 'stop' to match any stop combination in upper and lower case:

 >>> "Stop".lower() == "stop" True >>> "SToP".lower() == "stop" True >>> "sTOp".lower() == "stop" True 
+12
source

Here's more pythonic (Python 3) version.

 def guessing_game(random_num): tries = 3 print("Guess a number between 1 - 10, you have 3 tries, or type 'stop' to quit") while True: guess = input("Your number: ") try: guess = int(guess) except (TypeError, ValueError): if guess.lower() == 'stop' : return else: print("Invalid value '%s'" % guess) continue if guess == random_num: print('Correct') return elif guess < random_num: print('Too low') else: print('Too high') tries -= 1 if tries == 0: print('You have no more tries') return print('You have %s guesses left' % tries) 
+1
source

You are trying to convert the string "stop" to an integer. This string does not have a valid integer representation, so you get this error. You have to put

 if str(guess) == 'stop' or str(guess) == 'Stop': break 

as a first check

Another suggestion is to use lowercase letters at the input, and then check the lower case "stop". This way, you will need to check only once, and it will capture either β€œStop”, β€œSTOP”, β€œsTOp”, etc.

 if str(guess).lower() == 'stop': break 
0
source

Source: https://habr.com/ru/post/1482635/


All Articles