【问题标题】:why am i getting this error --> ( IndentationError: unexpected unindent )? [duplicate]为什么我会收到此错误--> ( IndentationError: unexpected unindent )? [复制]
【发布时间】:2020-06-28 01:39:32
【问题描述】:

编辑:这是整个代码https://pastebin.com/Njp9AsP4

这是给我错误的代码:

def save_file_as():
'''Save new file or save existing file with another name'''
filename = sg.popup_get_file('Save As', save_as=True, no_window=True)
try:
    if filename:
        file = pathlib.Path(filepath)
        file.write_text(values.get('_BODY_'))
        filename = filepath.split('/')
        window.TKroot.title(filename[-1] + '- memopad')
        return file
except:
    pass

这是错误:

[Running] python -u "d:\python\memopad v1.py"
  File "d:\python\memopad v1.py", line 51
    def save_file_as():
    ^
IndentationError: unexpected unindent

我第一次尝试用谷歌搜索,但只能找到遗漏除了:线女巫不是我的问题的人。

然后我尝试删除 try: 和 except:,令我惊讶的是错误仍然存​​在。

所以凌晨 2 点,我决定创建一个 stackoverflow 帐户,看看这里是否有人可以提供帮助。

其他信息:(我使用python3.8.3)(我使用windows 10)(我使用vscode)(我是一个编程的小伙伴)

【问题讨论】:

  • 向我们展示之前的代码——第 1-50 行
  • Python 关心代码如何缩进。空白很重要。许多其他程序只是使用 ( ) 或 { } 来定义子程序。您可能只是将整个部分缩进太多(或不够)。我建议参加一个快速的 Python 入门课程,比如在代码学院学习这些基础知识。祝你好运!
  • @sniperd 我 99% 确定所有缩进都正确我已经一遍又一遍地重写了 1% 只是因为错误显示“IndentationError”
  • 源文件中有混合制表符和空格?那会让你绊倒。尝试将制表符全局扩展为空格....
  • 你认为它的缩进是正确的。 Python 认为不是。我将接受 Python 的判断。

标签: python-3.x indentation


【解决方案1】:

这个错误的原因是你没有关闭之前代码中的try语句现在试试这个

def save_file(file):
    '''Save file instantly if already open; otherwise use `save-as` popup'''
    try:
        if file:
            file.write_text(values.get('_BODY_'))
        else:
            save_file_as()
    except:
        pass  
 
def save_file_as():
    '''Save new file or save existing file with another name'''
    filename = sg.popup_get_file('Save As', save_as=True, no_window=True)
    try:
        if filename:
            file = pathlib.Path(filepath)
            file.write_text(values.get('_BODY_'))
            filename = filepath.split('/')
            window.TKroot.title(filename[-1] + '- memopad')
            return file
    except:
        pass            

【讨论】:

  • 谢谢,但尝试后我可以说这并不能解决他的问题
  • 哪一行显示错误 51?请写下这段代码之前写的代码
  • pastebin 中的完整代码 id
  • 我现在修改了答案检查
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-22
  • 2016-01-01
  • 2019-11-22
相关资源
最近更新 更多