简体   繁体   English

缩进中制表符和空格的使用不一致

[英]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

I am new to Python.我是 Python 的新手。 The program pasted above gives me an error "Inconsistent use of tabs and spaces in indentation" at line "if temp == dna2:" specifically.上面粘贴的程序在“if temp == dna2:”行特别给我一个错误“缩进中制表符和空格的使用不一致”。 Can someone please help me out in finding out how the indentation is incorrect?有人可以帮我找出缩进是如何不正确的吗?

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.您必须修复它以与制表符或空格保持一致。

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.为避免将来出现这种情况,请训练自己仅使用制表键或空格键进行缩进,并考虑将编辑器设置为自动将制表符转换为空格。

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,最好将制表键设置为 4 个空格而不是“制表符”,这样您的问题就会减少,并且在您与其他人一起工作时这是一个很好的做法。

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 Lines such as $ symbol.这些空格将显示为行尾,例如$符号。 It's helpful.这很有帮助。

In my case Visual Studio code..在我的例子中,Visual Studio 代码..

Ctrl+Shift+P or View->Command Palette. Ctrl+Shift+P 或查看-> 命令面板。

Type类型

Convert Indentation to Spaces将缩进转换为空格

press Enter.按回车。

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另外,如果你不擅长“vim”编辑器中的缩进,那么在IDLE3中进行练习会很好

If you are copying and pasting code from another source, always copy and paste it on a normal text editor first, then fix the indentation after copying and pasting it in a code editor.如果您从其他来源复制和粘贴代码,请始终先将其复制并粘贴到普通文本编辑器中,然后在代码编辑器中复制和粘贴后修复缩进。 This is an easy way to fix this issue.这是解决此问题的简单方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM