Python ValueError:int() 的无效文字,基数为 10:'stop'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16742432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: invalid literal for int() with base 10: 'stop'
提问by Chris Pickett
Every time I try me code it works but when I type in 'stop'it gives me an error:
每次我尝试编写代码时它都可以工作,但是当我输入'stop'它时会出现错误:
ValueError: invalid literal for int() with base 10: 'stop'
ValueError:int() 的无效文字,基数为 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
采纳答案by Ashwini Chaudhary
The string passed to int()should only contain digits:
传递给的字符串int()应该只包含数字:
>>> 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 handlinghere:
一个快速的解决方法是在这里使用异常处理:
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 uppercase-lowercase combination of "stop":
您可以使用guess.lower() == 'stop'匹配“停止”的任何大写-小写组合:
>>> "Stop".lower() == "stop"
True
>>> "SToP".lower() == "stop"
True
>>> "sTOp".lower() == "stop"
True
回答by Pablo Mescher
You are trying to convert the string "stop" to an integer. That string has no valid representation as an integer, so you get that error. You should put
您正在尝试将字符串“stop”转换为整数。该字符串没有作为整数的有效表示,因此您会收到该错误。你应该把
if str(guess) == 'stop' or str(guess) == 'Stop':
break
as the first check
作为第一次检查
Another suggestion is to use lowercase on the input and then check for the lowercase 'stop'. That way you will have to check just once and it will capture either 'Stop', 'STOP', 'sTOp', etc..
另一个建议是在输入上使用小写字母,然后检查小写字母“stop”。这样,您只需检查一次,它将捕获“停止”、“停止”、“停止”等。
if str(guess).lower() == 'stop':
break
回答by bruno desthuilliers
Here's a more pythonic (Python 3) version.
这是一个更 Pythonic (Python 3) 的版本。
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)

