使用PythonAnywhere.com解释器的python3.7中的IndentationError

时间:2018-12-12 16:51:17

标签: python

我需要以下代码的帮助:

def CreateFile(shortcut, name):
 try:
    shortcut = open(name, 'x')
 except FileExistsError:
    print('That file already exists. Open it with OpenFile(shortcut, name).')
 else:
    print('File created. Open the file using OpenFile(shortcut, name).')

执行它时,出现以下错误:

  

IndentationError:unindent与任何外部缩进级别都不匹配

此错误代码指出[[FileExistsError除外]后的分号

我可以解决吗?这就是我看到其他人的代码并希望获得建议的方式。我正在创建自己的操作系统,其中存在OpenFile(快捷方式,名称)。

1 个答案:

答案 0 :(得分:0)

我前一段时间遇到了这个错误。发生这种情况是因为直接从pdf复制并粘贴了代码。我的建议是基本上重新编写可能解决问题的代码,或者似乎是问题的另一件事是函数缩进小于用于tryexcept的缩进,在两个地方都使用类似的缩进。

希望这有助于进度!