简体   繁体   English

Python中的缩进样式:反词功能

[英]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? 如何衡量python中的缩进级别?

You can look the Indentation Section in PEP8 : 您可以在PEP8中查看缩进部分

In general 一般来说

Use 4 spaces per indentation level. 每个缩进级别使用4个空格。

(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. 当开始一个新块的主体时(例如,在if和`while中),相对于引入该块的行,使主体缩进4个空格。

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有4个空格。 If there's another if , or while inside, you increase its body's indent 4 spaces further, and so on. 如果还有另一个if ,或者while内部,则进一步增加其主体的缩进4个空格,依此类推。


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. 一般规则是4空格缩进。

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). 缩进在python中很重要,因为它定义了哪些代码属于同一类(例如,用其他语言的方括号来完成)。

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. 注意,我还更改了j = len(word2)-1while j >= 0: :,因为长度为x的单词在位置0..x-1处具有字母。

Hint: string reversal can be achieved with slicing notation and a step size of -1: 提示:字符串反转可以通过切片符号和步长为-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. 在python中,缩进级别使用的空格数量在语法上并不重要,它可以是任意数字(在大多数情况下,每个级别使用2、4或8个空格),但必须保持一致。 So after an if block, you have to return to the same indentation level you had before, like this: 因此,在if块之后,您必须返回到以前的缩进级别,如下所示:

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. 此代码不会引发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: 您的代码也存在逻辑错误,因为j的起始值应比字符串的长度小1(由于从零开始的索引编制),然后一直下降到0,如下所示:

    i = 0
    j = len(word2) - 1

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

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

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