简体   繁体   English

Python:后面跟着 elif

[英]Python: while followed by elif

I thought else if and elif are logically identical.我认为else ifelif在逻辑上是相同的。

But when I try elif after while , I get a syntax error.但是当我在while之后尝试elif时,出现语法错误。

However, else is OK after while , as shown here (see comments A & B):但是,在while之后else是可以的,如此处所示(请参阅注释 A 和 B):

MENU_COMMANDS = {
'goal': 'the objective of the game is .... ',
'close': 'exit menu',
'menu': 'show all menu commands',
'quit': 'quit game' }

GAME_KEYBOARD = {
'1': 1,
'2': 2 }

def turn1():
key = input("Player 1, make your move")  # == 'goal'

while key in MENU_COMMANDS:
    menu(key)
    key = input("after exiting the menu, make your move")
else:  # why not 'elif' ?    COMMENT A
    if key not in GAME_KEYBOARD:
        print("invalid move")
        return False

# elif key not in GAME_KEYBOARD:  # why won't this work? :(    COMMENT B
#     print("invalid move")
#     return False

Is this the only way to execute this logic, or is there a better one?这是执行此逻辑的唯一方法,还是有更好的方法?

Thank you!谢谢!

elif: ... is equivalent to else: if: ... in the context of an if block, but the else in a while block has an entirely different meaning from the else in an if block, so the two aren't interchangeable even though they're represented by the same keyword. elif: ...等同于else: if: ...if块的上下文中,但是while块中的elseif块中的else具有完全不同的含义,因此两者不可互换即使它们由相同的关键字表示。

The else in this code block is unnecessary anyway:这个代码块中的else无论如何都是不必要的:

while key in MENU_COMMANDS:
    menu(key)
    key = input("after exiting the menu, make your move")
else:  # why not 'elif' ?    COMMENT A
    if key not in GAME_KEYBOARD:
        print("invalid move")
        return False

Since you never break your while , there's no circumstance under which you'd end the loop and not enter the else .由于您永远不会break while ,因此在任何情况下您都不会结束循环而不进入else Hence you can just remove the else and it should behave the same:因此,您可以删除else ,它的行为应该相同:

while key in MENU_COMMANDS:
    menu(key)
    key = input("after exiting the menu, make your move")

if key not in GAME_KEYBOARD:
    print("invalid move")
    return False

For loop control, else is a special case vs standard conditional statement.对于循环控制,与标准条件语句相比, else是一种特例。

When used with a loop, the else clause has more in common with the else clause of a try statement than it does with that of if statements: a try statement's else clause runs when no exception occurs, and a loop's else clause runs when no break occurs.当与循环一起使用时,else 子句与 try 语句的 else 子句比与 if 语句的有更多共同点:try 语句的 else 子句在没有异常发生时运行,而循环的 else 子句在没有 break 时运行发生。

https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

>>> c = 0
>>> while c < 10:
...    break
... else:
...    print("caught a break")
...
>>> while c < 10:
...    c+=2
... else:
...    print("no break")
...
no break
if a:
    do something
elif b:
    do something
elif c:
    do something 
else:
    ouch

is the same as是相同的

if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do these
if (not a) and (not b) and (not c):
    no

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

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