我有一个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这些错误意味着什么?我究竟做错了什么?我该如何修复我的代码?
def contains_sequence(dna1, dna2):
''' (str, str) -> bool
Return True if and only if DNA sequence dna2 occurs in the DNA sequence
dna1.
>>> contains_sequence('ATCGGC', 'GG')
True
>>> contains_sequence('ATCGGC', 'GT')
False
'''
b=False
len2=len(dna2)
i=0
for j in dna1:
temp=dna1[i:i+len2]
if temp == dna2:
b=True
i=i+1
return b
Run Code Online (Sandbox Code Playgroud)
我是Python的新手.上面粘贴的程序在"if temp == dna2:"行中给出了一个错误"在缩进中使用制表符和空格的不一致".有人可以帮我找出缩进是如何不正确的吗?
为新手问题道歉,但我已经阅读了手册,这个问题,并尝试了几次没有我预期的结果。
所以我使用vim来编辑一个文件(附后)。但是在运行时,我得到了 TabError:在缩进错误中不一致使用制表符和空格。
这是我尝试过的:
:retab,和:x。再次运行该文件。仍然收到 TabError 消息。:retab!和:x。再次运行该文件。仍然收到 TabError 消息。:retab! 4和:x。再次运行该文件。这次它有效,但我不知道为什么?另外,文件缩进似乎过长。(我在这里读到编辑器可能会为一个选项卡显示 8 个空格)我的问题是:
这是什么:retab,:retab!以及:retab! 4是什么意思?
为什么:retab对我的文件不起作用?
#!/usr/bin/env python
#Reduce function for computing matrix multiply A*B
#Input arguments:
#variable n should be set to the inner dimension of the matrix product (i.e., the number of columns of A/rows of B)
import sys
import …Run Code Online (Sandbox Code Playgroud)