【问题标题】:try:/except: returns a syntax error in Python when trying to run module [closed]try:/except: 尝试运行模块时在 Python 中返回语法错误 [关闭]
【发布时间】:2019-09-21 19:24:08
【问题描述】:

下面是我的代码,错误在第 4 行,“except”给出了一个语法错误...(这是一个调试和错误捕获分配。我看不出它有什么问题)

while True:
        try:
                userInputOne = input(int("How much time in hours a week, do you spend practicing? ")
        except TypeError:
                print("Oops! Practice time must be rounded to the nearest integer. It also needs to be a numerical value! ")
                break
    else:
        userInputTwo = str(input"How good to do want to be? Enter 'very good', 'good', mediocre, 'not good' ")
        if userInputTwo not in ('very good', 'good', 'mediocre', 'not good'):
            print("Please use one of the options. ")
        else:
            print("Let's calculate...")
            break

【问题讨论】:

  • 你的括号不平衡。
  • 还有破碎的缩进。
  • 我如何找到或更正它?取消缩进并重做?
  • 它在userInputOne 行中,但是您也应该能够通过阅读错误消息的回溯来找到它。他们通常指出错误在哪里
  • 这是家庭作业吗?为什么你的任务是在 Python 中似乎很少有经验?

标签: python syntax except


【解决方案1】:

我附上了工作代码。 语法错误是由缺少括号和错误的缩进引起的。看看你的else: 声明。它与try: 声明的高度不同。 TypeError 意味着,您不必将输入转换为字符串,因为它们已经是。否则我建议你创建一些变量并在你想用它们计算时通过int() 转换它们。

while True:
    try:
        userInputOne = input("How much time in hours a week, do you spend practicing? ")
    except TypeError:
        print("Oops! Practice time must be rounded to the nearest integer. It also needs to be a numerical value! ")
        break
    else:
        userInputTwo = input("How good to do want to be? Enter 'very good', 'good', mediocre, 'not good' ")
        if userInputTwo not in ('very good', 'good', 'mediocre', 'not good'):
            print("Please use one of the options. ")
        else:
            print("Let's calculate...")
            break

编辑: 我推荐使用 PyCharm(如果你不这样做的话),它的 Auto-Indent 功能和漂亮的“缩进指南”。所以你可以更容易地看到许多错误。

【讨论】:

  • 我会得到 PyCharm。我听说过,但不确定它是否免费。
  • 有一个免费版本的 PyCharm。我强烈推荐它。过了一段时间,我实际上也发现了付费版本的好处。
【解决方案2】:

你的代码:

while True:
        try:
                userInputOne = input(int("How much time in hours a week, do you spend practicing? ")
        except TypeError:
                print("Oops! Practice time must be rounded to the nearest integer. It also needs to be a numerical value! ")
                break
    else:
        userInputTwo = str(input"How good to do want to be? Enter 'very good', 'good', mediocre, 'not good' ")
        if userInputTwo not in ('very good', 'good', 'mediocre', 'not good'):
            print("Please use one of the options. ")
        else:
            print("Let's calculate...")
            break

首先,应该如下所示:

while True:
    try:
        userInputOne = input(int("How much time in hours a week, do you spend practicing? "))
    except TypeError:
        ...
        ...

我喜欢用一些方法来捕捉和解决这个问题:

  • 在您的文件上运行pylint。它会告诉你哪里存在错误,并警告你可以改进的代码。
  • 使用 vim 和命令== 会尝试做一些自动缩进。

但最重要的是了解whitespace is important in Python。整个文件中存在空格的语法错误。您的代码中需要有四个空格而不是八个。此外,如上面的 cmets 所述,您有一些不平衡的括号会破坏您的代码。此外,您有一个没有ifelse 语句。周围有很多问题,所以我建议一次重新编写几行代码,并确保它在继续之前可以正常工作。此外,您无法将字符串转换为 int,或者至少会得到意想不到的结果。

【讨论】:

  • 我确定 input(int( ... 不正确。
  • 好收获!提供的代码中有很多错误。似乎 OP 在没有太多 Python 经验的情况下被投入到这项任务中。
  • 同意!斯科特 S.
  • 是的。在意识到您指出这些之前,我实际上已经注意到了。我知道它应该是 int(input( 但是我猜我只是忽略了它,因为它们都以“i”开头。它现在 100% 工作。谢谢!
  • 高兴@hbss。希望随着您获得更多使用 Python 的经验,这些错误更容易脱颖而出。如果它也有帮助,我会投赞成票;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-18
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
相关资源
最近更新 更多