简体   繁体   中英

Indentation Style in Python : Reverse Words Function

def is_reverse(word1, word2):
    if len(word1) != len(word2):
             return False
           i = 0
           j = len(word2)

    while j > 0:
                  if word1[i] != word2[j]:
                            return False
                    i = i+1
                    j = j-1

               return True

is_reverse('pots', 'stop')

I had defined above function to check two given words reversely match to each other.

but when I run it, it keeps reminding me of indentation error.

How to gauge the indentation level in python?

You can look the Indentation Section in PEP8 :

In general

Use 4 spaces per indentation level.

(don't mix tabs and spaces).

When you start the body of a new block (eg, in your if and `while), keep the body indented 4 spaces relative to the line introducing the block.

So when you write

if len(word1) != len(word2):
    ...
    ...

Everything that should happen if the condition occurred, should have 4 spaces relative to the if , for example. If there's another if , or while inside, you increase its body's indent 4 spaces further, and so on.


As a side note, note that you can check if two words are reverse matches of each other with

def is_reverse(word1, word2):
    return word1 == reversed(word2)

General rule is 4 space indentation.

Below is an example of proper indentation, without changes to the original code:

def is_reverse(word1, word2):
    if len(word1) != len(word2):
        return False
    i = 0
    j = len(word2)

    while j > 0:
        if word1[i] != word2[j]:
            return False
        i = i+1
        j = j-1

    return True

Indentation matters in python, because it defines which code belongs together (which is done eg with brackets in other languages).

Because your code is heavily malformed, please have a look at the reformatted code:

def is_reverse(word1, word2):
    if len(word1) != len(word2):
        return False
    i = 0
    j = len(word2)-1

    while j >= 0:
        if word1[i] != word2[j]:
            return False
        i = i+1
        j = j-1
    return True

is_reverse('pots', 'stop')
-> True

Notice I also changed j = len(word2)-1 and while j >= 0: , since a word of length x has letters in positions 0..x-1.

Hint: string reversal can be achieved with slicing notation and a step size of -1:

"pots"[::-1] == "stop"

The number of spaces used for an indentation level in python is not syntactically important, it can be any arbitrary number (in most cases 2, 4 or 8 spaces are used per level), but it has to be consistent. So after an if block, you have to return to the same indentation level you had before, like this:

def is_reverse(word1, word2):
    if len(word1) != len(word2):
             return False

    i = 0
    j = len(word2)

    while j > 0:
                  if word1[i] != word2[j]:
                            return False
                  i = i+1
                  j = j-1

    return True

is_reverse('pots', 'stop')

This code won't throw an IndentationError. Of course, for stylistic reasons, it's good to have a constant indentation level, like this:

def is_reverse(word1, word2):
    if len(word1) != len(word2):
        return False

    i = 0
    j = len(word2)

    while j > 0:
        if word1[i] != word2[j]:
            return False
        i = i+1
        j = j-1

    return True

is_reverse('pots', 'stop')

Your code also has a logical error, as the starting value of j should be 1 less than the length of the string (due to zero-based indexing), and go down all the way to 0, like this:

    i = 0
    j = len(word2) - 1

    while j >= 0:
        if word1[i] != word2[j]:
            return False
        i = i+1
        j = j-1

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