如何在 Python 中縮排多個 if...else 語句?
使用縮排塊是 Python 的一項重要特性。塊的縮排級別高於之前的語句。因此,如果程式中有多個 if 語句以巢狀的方式出現,那麼每個後續的縮排塊的縮排級別都會增加。
if expr1==True: if expr2==True: stmt1 else: if expr3==True: stmt2 else: if expr3==True: stmt3 else: stmt4
廣告
使用縮排塊是 Python 的一項重要特性。塊的縮排級別高於之前的語句。因此,如果程式中有多個 if 語句以巢狀的方式出現,那麼每個後續的縮排塊的縮排級別都會增加。
if expr1==True: if expr2==True: stmt1 else: if expr3==True: stmt2 else: if expr3==True: stmt3 else: stmt4