简体   繁体   中英

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. 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?

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.

I was almost struct at this problem for quiet some time. I was using CentOS Ec2 and found out that you can:

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..

Ctrl+Shift+P or View->Command Palette.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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