Python 缩进中制表符和空格的使用不一致
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12989171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inconsistent use of tabs and spaces in indentation
提问by knightcool
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
I am new to Python. The program pasted above gives me an error "Inconsistent use of tabs and spaces in indentation" at line "if temp == dna2:" specifically. Can someone please help me out in finding out how the indentation is incorrect?
我是 Python 的新手。上面粘贴的程序在“if temp == dna2:”行特别给了我一个错误“在缩进中使用制表符和空格不一致”。有人可以帮我找出缩进是如何不正确的吗?
采纳答案by poke
It means you have mixed up spaces and tabs in the indentation. You have to fix that to be consistent with either tabs or spaces.
这意味着您在缩进中混淆了空格和制表符。您必须修复它以与制表符或空格保持一致。
回答by razi
Assuming you have a "good" IDE, it's best to set the tab key to make 4 spaces instead of a "tab", that way you have less problems, and it's good practice, for when you will work with other people.
假设您有一个“好”的 IDE,最好将 Tab 键设置为 4 个空格而不是一个“制表符”,这样您的问题就会更少,而且这是一个很好的做法,因为您何时与其他人一起工作。
回答by Larry Lustig
If you look carefully at the lines
如果你仔细看线条
temp=dna1[i:i+len2]
if temp == dna2:
in your code, you will see that the "space" at the beginning of each line is "constructed" differently. In one case it uses tabs and in the other spaces, or, if both have tabs and spaces then they are used in different combinations.
在您的代码中,您会看到每行开头的“空间”的“构造”不同。在一种情况下,它使用制表符并在其他空格中使用,或者,如果两者都有制表符和空格,则它们以不同的组合使用。
You can examine this by placing your cursor at the beginning of each line and using the right-arrow key to "walk" your way through the characters. You'll see that the cursor moves differently on each line.
您可以通过将光标放在每行的开头并使用右箭头键“遍历”字符来检查这一点。您会看到光标在每一行上的移动方式不同。
To fix, delete the tabs and spaces at the beginning of each line and re-insert them with the same characters on each line.
要修复,请删除每行开头的制表符和空格,然后在每行上重新插入相同的字符。
To avoid in the future, train yourself to use only the tab key OR the space key to indent, and consider setting your editor to automatically convert tabs to spaces.
为避免将来发生,请训练自己仅使用 Tab 键或空格键进行缩进,并考虑将编辑器设置为自动将制表符转换为空格。
回答by D3vil_Mind
According to the your Doc strings
根据您的文档字符串
your code:
你的代码:
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
This much Big code can be simplified to one line
这么多大代码可以简化为一行
return dna1.find(dna2)>=0
Also if u are not good with indentations in 'vim' editor its good to practice in IDLE3
回答by Vritansh Kamal
I was almost struct at this problem for quiet some time. I was using CentOS Ec2 and found out that you can:
我几乎在这个问题上安静了一段时间。我正在使用 CentOS Ec2 并发现您可以:
vim <filename> Press Escape Key If you're in write/insert mode :set list
The spaces will be visible as End Of Linessuch as $symbol.
It's helpful.
空格将显示为行尾,例如$符号。这很有帮助。

