<tfoot id='e83Hy'></tfoot>

<small id='e83Hy'></small><noframes id='e83Hy'>

    <bdo id='e83Hy'></bdo><ul id='e83Hy'></ul>
<i id='e83Hy'><tr id='e83Hy'><dt id='e83Hy'><q id='e83Hy'><span id='e83Hy'><b id='e83Hy'><form id='e83Hy'><ins id='e83Hy'></ins><ul id='e83Hy'></ul><sub id='e83Hy'></sub></form><legend id='e83Hy'></legend><bdo id='e83Hy'><pre id='e83Hy'><center id='e83Hy'></center></pre></bdo></b><th id='e83Hy'></th></span></q></dt></tr></i><div id='e83Hy'><tfoot id='e83Hy'></tfoot><dl id='e83Hy'><fieldset id='e83Hy'></fieldset></dl></div>

      1. <legend id='e83Hy'><style id='e83Hy'><dir id='e83Hy'><q id='e83Hy'></q></dir></style></legend>

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

        时间:2023-09-11
        <i id='og3Ol'><tr id='og3Ol'><dt id='og3Ol'><q id='og3Ol'><span id='og3Ol'><b id='og3Ol'><form id='og3Ol'><ins id='og3Ol'></ins><ul id='og3Ol'></ul><sub id='og3Ol'></sub></form><legend id='og3Ol'></legend><bdo id='og3Ol'><pre id='og3Ol'><center id='og3Ol'></center></pre></bdo></b><th id='og3Ol'></th></span></q></dt></tr></i><div id='og3Ol'><tfoot id='og3Ol'></tfoot><dl id='og3Ol'><fieldset id='og3Ol'></fieldset></dl></div>
        1. <legend id='og3Ol'><style id='og3Ol'><dir id='og3Ol'><q id='og3Ol'></q></dir></style></legend>
        2. <tfoot id='og3Ol'></tfoot>
          • <small id='og3Ol'></small><noframes id='og3Ol'>

              <tbody id='og3Ol'></tbody>

                <bdo id='og3Ol'></bdo><ul id='og3Ol'></ul>

                  本文介绍了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'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 Python 中将字符串转换为 int? 下一篇:我的输入怎么不等于答案?

                  相关文章

                  最新文章

                      <bdo id='S2s8l'></bdo><ul id='S2s8l'></ul>
                  1. <i id='S2s8l'><tr id='S2s8l'><dt id='S2s8l'><q id='S2s8l'><span id='S2s8l'><b id='S2s8l'><form id='S2s8l'><ins id='S2s8l'></ins><ul id='S2s8l'></ul><sub id='S2s8l'></sub></form><legend id='S2s8l'></legend><bdo id='S2s8l'><pre id='S2s8l'><center id='S2s8l'></center></pre></bdo></b><th id='S2s8l'></th></span></q></dt></tr></i><div id='S2s8l'><tfoot id='S2s8l'></tfoot><dl id='S2s8l'><fieldset id='S2s8l'></fieldset></dl></div>
                  2. <small id='S2s8l'></small><noframes id='S2s8l'>

                    <tfoot id='S2s8l'></tfoot>
                      <legend id='S2s8l'><style id='S2s8l'><dir id='S2s8l'><q id='S2s8l'></q></dir></style></legend>