Python:if-endif 语句在哪里结束?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19430190/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:43:36  来源:igfitidea点击:

Python: Where does if-endif-statement end?

pythonvb.netvb6migration

提问by tmighty

I have the following code:

我有以下代码:

for i in range(0,numClass):
    if breaks[i] == 0:
        classStart = 0
    else:
        classStart = dataList.index(breaks[i])
        classStart += 1
classEnd = dataList.index(breaks[i+1])

classList = dataList[classStart:classEnd+1]

classMean = sum(classList)/len(classList)
print classMean
preSDCM = 0.0
for j in range(0,len(classList)):
    sqDev2 = (classList[j] - classMean)**2
    preSDCM += sqDev2

SDCM += preSDCM
return (SDAM - SDCM)/SDAM

I would like to convert this code to VB.NET.

我想将此代码转换为 VB.NET。

But I am not sure where the if-elseif-statement ends. Does it end after "classStart += 1"?

但我不确定 if-elseif 语句在哪里结束。它是否在“classStart += 1”之后结束?

I feel it a bit difficult to see where the for-next-loops end as well in Python.

我觉得在 Python 中很难看到 for-next 循环的结束位置。

The code is taken from http://danieljlewis.org/files/2010/06/Jenks.pdf

代码取自http://danieljlewis.org/files/2010/06/Jenks.pdf

Thank you.

谢谢你。

采纳答案by Daniel Roseman

Yes. Python uses indentation to mark blocks. Both the ifand the forend there.

是的。Python 使用缩进来标记块。那里iffor终点都在那里。

回答by Akshat Tripathi

In Python, where your indented block ends, that is exactly where your block will end. So, for example, consider a bit simpler code:

在 Python 中,您的缩进块结束的地方,也就是您的块将结束的地方。因此,例如,考虑一个更简单的代码:

myName = 'Jhon'
if myName == 'Jhon':
   print(myName * 5)
else:
   print('Hello')

Now, when you run this code (make sure to run it from a separate module, not from the interactive prompt), it will print 'Jhon' five times (note that Python will treat the objects exactly as they are specified, it won't even bother to try to convert the variable myName's value to a number for multiplication) and that's it. This is because the code block inside of the if block is only executed. Note that if the elsekeyword was put anywhere but just below the if statement or if you had mixed the use of tabs and spaces, Python would raise an error.

现在,当您运行此代码时(确保从单独的模块运行它,而不是从交互式提示中运行),它将打印 'Jhon' 五次(请注意,Python 将完全按照指定的方式处理对象,它不会”甚至懒得尝试将变量myName的值转换为乘法的数字),就是这样。这是因为 if 块内的代码块只被执行。请注意,如果将else关键字放在 if 语句下方的任何位置,或者如果您混合使用制表符和空格,Python 将引发错误。

Now, in your code,

现在,在您的代码中,

for i in range(0,numClass):
    if breaks[i] == 0:
       classStart = 0
    else:
       classStart = dataList.index(breaks[i])
       classStart += 1

See, where the indent of for's block of code starts? One tab, so, everything indented one tab after the forstatement, will be inside of the for block. Now, obviously, the ifstatement is inside of the forstatement, so it's inside the forstatement. Now, let's move to next line, classStart = 0-- this is indented two tabs from the forstatement and one tab from the ifstatement; so it's inside the ifstatement andinside the for block. Next line, you have an elsekeyword indented just one tab from the forstatement but not two tabs, so it's inside the forstatement, and notinside the ifstatement.

看,for代码块的缩进从哪里开始?一个制表符,因此,在for语句之后缩进一个制表符的所有内容都将在 for 块内。现在,很明显,if语句在for语句内,所以它在for语句内。现在,让我们移到下一行,classStart = 0--这是for语句中的两个制表符和语句中的一个制表符缩进if;所以它的内部if声明内部块。下一行,您有一个else关键字从for语句中缩进了一个制表符,而不是两个制表符,因此它在for语句内部,而不是if语句内部。

Consider putting curly-braces like these if you have coded in another language(s) before:

如果您之前曾用另一种语言进行编码,请考虑使用这样的花括号:

for i in range(0,numClass)
{
    if breaks[i] == 0
        {
        classStart = 0
        }
    else
        {
        classStart = dataList.index(breaks[i])
        classStart += 1
        }
}

The simple differences are that you are not required to put parenthesis for your expressions, unless, you want to force operator precedence rule and you don't need those curly braces, instead, just indent them equally.

简单的区别是您不需要为表达式添加括号,除非您想强制运算符优先规则并且您不需要那些花括号,相反,只需等价缩进它们即可。

回答by user13289173

for i in range(0,numClass):
    if breaks[i] == 0:
       classStart = 0
    else:
       classStart = dataList.index(breaks[i])
       classStart += 1
#   end if