【问题标题】:Why am I getting syntax error on 'excpet'?为什么我在“除外”上出现语法错误?
【发布时间】:2017-11-03 08:33:29
【问题描述】:

我不知道为什么在“excpet”上出现语法错误。在我看来一切都很好!这是我的代码:

def length():
gameLength = int(input("How many words do you want to play? You can chose anywhere from 1-40: "))
global gameLength
if gameLength <= 40 and gameLength >= 1:
    quit
else:
    int(input("Please choose a number between 1 & 40 "))
except ValueError = True:
     int(input("Please choose a number between 1 & 40 "))
return gameLength

【问题讨论】:

  • 可能是因为你打错了'except'?
  • 我很惊讶你只收到一个错误那里。使用您展示的代码,正如您展示的那样,您应该首先遇到许多其他错误。假设这是 Python 当然(不要忘记语言标签!)。
  • 另外,在发布有关错误的问题时,请将实际的错误输出复制粘贴到问题中。作为文本,完整、完整且未经编辑。
  • 最后,请read more about exceptions,因为你没有正确使用它。

标签: python syntax compiler-errors except


【解决方案1】:

你需要正确缩进你的代码,并在except之前添加一个try语句。您还需要使用 '==' 而不是 '=' 来评估 true。

def length():
    global gameLength
    gameLength = int(input("How many words do you want to play? You can chose anywhere from 1-40: "))
    if gameLength <= 40 and gameLength >= 1:
        quit
    else:
        try:
            int(input("Please choose a number between 1 & 40 "))
        except ValueError == True:
            int(input("Please choose a number between 1 & 40 "))
    return gameLength

【讨论】:

    【解决方案2】:

    解决方案:
    首先,你必须缩进你的函数(不是主要错误)。现在继续下一个错误: 1.在有一个except之前你必须有一个try语句。此外,您应该使用 return ValueError,而不是重复相同的功能。在这里,我修改了代码,但按照您的要求进行了修改:

    try:
        if gameLength <= 40 and gameLength >= 1:
            quit
        else:
            return ValueError
    except ValueError: // or except ValueError == true
        length()
    
    1. 查看带有except ValueError = true: 的行。一个等号表示您将 ValueError 分配为等于 true,就像您说的 x = 10 一样。两个等号表示您在问“它是否等于真?”,例如说
      1 + 5 == 6 将返回真,因为 1+5 是 6。现在回到您的 ValueError。

      将您的 except 行更改为:except ValueError == true。现在您要问,“ValueError 是否等于 true?”而不是说,“ValueError 必须等于 true!”你也可以说except ValueError,因为 if 语句和其他语句总是在返回 true 时执行。例如,if true: 将起作用或 while true 将永远持续下去,但 if 1+1==3: 永远不会起作用,因为 1+1==3 返回 false。

    2. 先将 gameLength 分配为全局变量,然后再为其分配值。

    3. 将gameLength放在try语句中:

      try:
          gameLength = int(input("How many..."))
      

      为什么?因为如果你把它放在 try 语句之前并且输入是,例如,“hi”,你不能让“hi”成为一个整数。尝试语句 try 做某事,失败后不只是放弃并返回错误,而是做程序员希望它做的事情。在这种情况下,我们希望它重复该功能,因此我们将对异常执行 length()。这就是

    4. 一遍又一遍地重复这个函数。

      except ValueError:
          print("Please type a number from 1-40.")
          length()
      

      最后,我再改变一件事:

    5. quit 是一个函数。让它退出(),否则它不会像你预期的那样退出。

    输出(和代码)

    def length():
        global gameLength
    
        try:
            gameLength = int(input("How many words do you want to play? You can choose any number from 1-40: "))
            if gameLength <= 40 and gameLength >= 1:
                quit()
            else:
                return ValueError
        except ValueError:
             print("Please type a number from 1-40.")
             length()
        return gameLength
    
    length()
    

    IDLE:你想玩多少字?您可以选择 1-40 之间的任何数字:
    ME:“我是一个字符串。”
    IDLE:请输入 1-40 之间的数字.
    IDLE:你想玩多少字?您可以选择 1-40 之间的任何数字:
    ME:54919
    IDLE:请输入 1-40 之间的数字。
    IDLE :你想玩多少字?您可以从 1-40 中选择任意数字:
    ME:37.1
    *将数字设为整数:37.1 => 37。
    CON:37
    退出 Python。

    【讨论】:

      猜你喜欢
      • 2019-07-07
      • 2022-01-18
      • 2018-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 2013-01-18
      • 2021-01-25
      相关资源
      最近更新 更多