简体   繁体   English

Python 中语句“elif”的含义

[英]Meaning of statement “elif” in Python

In the following code i want to know what exactly is the meaning of the elif statement.I know that if the "if" statement gives false then the statements in the "elif" statement will occur.However i wanna know exactly which is the false condition from the "if" statement(if it is false).Is being continued in my comment在下面的代码中,我想知道 elif 语句的确切含义。我知道如果“if”语句给出错误,那么“elif”语句中的语句将会发生。但是我想确切地知道哪个是错误的“if”语句中的条件(如果它是假的)。在我的评论中继续

def find_Biggest():      
#function definition
    if(a>b) and (a>c):
        largest=a
    elif(b>a) and (b>c):
        largest=b
    else:
        largest=c

The condition in the "if" statement is (a>b and a>c) .If this condition is false it means that either a<b either a<c or (a<b and a<c) right? “if”语句中的条件是(a>b and a>c) 。如果此条件为假,则意味着a<b要么a<c要么(a<b and a<c)对吗? So,the condition in the following "elif" must be the opposite of thaf condition..Which is the opposite of this condition?Sorry if i confused you i didn't mean to but though i can solve this easy example i wanna understand the inner mechanism of the if -elif..thank you very much.因此,以下“elif”中的条件必须与 thaf 条件相反。与此条件相反的是什么?对不起,如果我让您感到困惑,我不是故意的,但尽管我可以解决这个简单的例子,但我想了解if -elif 的内部机制..非常感谢。

As others have said, it is the same as if there was else if .正如其他人所说,它就像有else if One very important difference, though: it is there to solve the so called Dangling else problem.但是,一个非常重要的区别是:它可以解决所谓的Dangling else问题。 In particular, the problem in itself is solved by Python's white-space sensitive syntax, but the parser disallows else if syntax because of it.特别是,问题本身已通过 Python 的空格敏感语法解决,但解析器因此不允许else if语法。

If any of the mentioned condition in the "if" clause is false,it will move forward to "elif" it does't matter now what were the condition earlier,it will simply check the new conditions.如果“if”子句中提到的任何条件为假,它将前进到“elif”,现在不管之前的条件是什么,它只会检查新的条件。 If all conditions are true(in elif) it will simply execute the statements.It is not an issue of being opposite to "if".如果所有条件都为真(在 elif 中),它将简单地执行语句。这不是与“if”相反的问题。

elif is the python equivalent of "else if". elif 是“else if”的 python 等效项。

elif means that execution will happen if and only if the conditions above it are not satisfied and the elif itself is satisfied - it is like a switch / case statement in plain English some programming languages. elif意味着当且仅当它上面的条件不满足并且elif本身满足时才会执行——它就像一些编程语言的简单英语中的switch / case语句。 It will also not goto to the else condition in this case.在这种情况下,它也不会转到else条件。

Here you are checking if a is the biggest - if so it marks it - otherwise it checks if b is the biggest and mark it;在这里,您正在检查a是否最大 - 如果是,则标记它 - 否则它检查b是否最大并标记它; otherwise it marks c as the biggest (even if it tied with a or b ).否则它将c标记为最大的(即使它与ab绑定)。

More specifically, if a>b and a>c is false that just means a is not the biggest (it could be tied with b or c , but that is not enough).更具体地说,如果a>b and a>c为假,则仅意味着a不是最大的(它可以与bc ,但这还不够)。 Similarly if b>a and b>c is false, that means b is not the biggest (again tie is not enough).同样,如果b>a and b>c为假,则意味着b is not the biggest (同样 tie 是不够的)。 If both are false, it marks c as the biggest even if it is tied.如果两者都是假的,即使它并列,它也会将c标记为最大。

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

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