我有一个Python脚本:
if True:
if False:
print('foo')
print('bar')
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行我的脚本时,Python提出了一个IndentationError:
File "script.py", line 4
print('bar')
^
IndentationError: unindent does not match any outer indentation level
Run Code Online (Sandbox Code Playgroud)
我一直在玩我的程序,我还能够产生其他三个错误:
IndentationError: unexpected indentIndentationError: expected an indented blockTabError: inconsistent use of tabs and spaces in indentation这些错误意味着什么?我究竟做错了什么?我该如何修复我的代码?
我想将一段代码复制并粘贴到我的Python解释器中.不幸的是,由于Python对空白的敏感性,以一种有意义的方式复制和粘贴它并不简单.(我认为空白被破坏了)有更好的方法吗?也许我可以从文件中加载片段.
这只是一个小例子,但是如果有很多代码我想避免从函数的定义或复制和逐行粘贴中输入所有内容.
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def disable(self):
self.HEADER = '' # I think stuff gets mangled because of the extra level of indentation
self.OKBLUE = ''
self.OKGREEN = ''
self.WARNING = ''
self.FAIL = ''
self.ENDC = ''
Run Code Online (Sandbox Code Playgroud) 当我运行以下代码时,这些代码在函数内部具有空白行(无空格),在解释器模式下逐行运行代码时,我得到的行为与Python 3.6.5不同python3 split.py:
# File split.py
def find(s, start, predictor):
for i in range(start, len(s)):
if predictor(s[i]):
return i
return -1
def find_all(s, sep=" \t\n"):
beg_of_nonsep = 0
while beg_of_nonsep < len(s):
beg_of_nonsep = find(s, beg_of_nonsep, lambda ch, sep_chs=sep: ch not in sep_chs)
if beg_of_nonsep == -1:
break
end_of_nonsep = find(s, beg_of_nonsep + 1, lambda ch, sep_chs=sep: ch in sep_chs)
if end_of_nonsep == -1:
end_of_nonsep = len(s)
yield (beg_of_nonsep, end_of_nonsep)
beg_of_nonsep = end_of_nonsep + 1
split = lambda …Run Code Online (Sandbox Code Playgroud)