【问题标题】:my code is throwing a indent error and i cant find the problem我的代码抛出缩进错误,我找不到问题
【发布时间】:2020-05-05 00:32:06
【问题描述】:

我正在使用 pyqt5 模块,它在这里抛出一个缩进错误是抛出错误的代码位

我试图将块放在文本浏览器变量之后

from PyQt5 import QtCore, QtGui, QtWidgets

 with open("data.txt", "a+") as data_file:
 try:
     for text in data_file:
         textBrowser.append(text)
 else:
     for texts in data_file:
         textBrowser.append(texts)`

【问题讨论】:

  • 查看源代码视图,您似乎没有缩进 with 正文,并且在您的大多数行中都有一个奇怪的额外前导空格,可能已经或可能没有引入尝试发布此内容的过程。此外,您的try 没有exceptfinally,也不清楚为什么您有try
  • Python 对缩进比较挑剔,请看PEP8
  • 理解python的相关代码编辑器会捕捉到这个错误。优秀的tutorial 应该会有所帮助。

标签: python pyqt5


【解决方案1】:

我不确定您的缩进,但 try-else IMO 的正确形式是 try-except-else。像这样:

try:
    ...
except Exception:
    ...
else:
    ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    相关资源
    最近更新 更多