ValueError: int() 以 10 为底的无效文字:'stop'

ValueError: invalid literal for int() with base 10: #39;stop#39;(ValueError: int() 以 10 为底的无效文字:stop)
本文介绍了ValueError: int() 以 10 为底的无效文字:'stop'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

每次我尝试编写代码时它都可以工作,但是当我输入 'stop' 时,它会给我一个错误:

Every time I try me code it works but when I type in 'stop' it gives me an error:

ValueError: int() 以 10 为底的无效文字:'stop'

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

推荐答案

传递给 int() 的字符串应该只包含数字:

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

>>> 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 here:

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()

您可以使用 guess.lower() == 'stop' 来匹配stop"的任何大小写组合:

And you can use guess.lower() == 'stop' to match any uppercase-lowercase combination of "stop":

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

这篇关于ValueError: int() 以 10 为底的无效文字:'stop'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

build conda package from local python package(从本地 python 包构建 conda 包)
How can I see all packages that depend on a certain package with PIP?(如何使用 PIP 查看依赖于某个包的所有包?)
How to organize multiple python files into a single module without it behaving like a package?(如何将多个 python 文件组织到一个模块中而不像一个包一样?)
Check if requirements are up to date(检查要求是否是最新的)
How to upload new versions of project to PyPI with twine?(如何使用 twine 将新版本的项目上传到 PyPI?)
Why #egg=foo when pip-installing from git repo(为什么从 git repo 进行 pip 安装时 #egg=foo)