如何在python中实现多个嵌套if

问题描述 投票:0回答:1

我想制作一个具有多个嵌套 if 的程序,但我找不到在 Python 中执行此操作的可读或好的方法。我附上一张带有我的程序流程的图像;例如,我有很多 if 语句。
我可以实现这个,但是代码很难读。
是否存在以可读的 Python 代码实现此流程图的形式或方法?

Flowchart of my logic to implement in python code

python if-statement flowchart nested-if
1个回答
0
投票

这是一个基本练习,旨在了解您是否了解如何使用

if
else
elif
语句

# assume a condition to start the statement
    
a == True
    
if a:# this indicates if a == True

    if func_1() and func_2(): # combining 2 consecutive True  statements
        func_4()
        
    elif func_1() and not func_2(): #Combining if first statement is True and the 2nd statement is False
        func_3()

    elif func_4():# Use elif as there is no further nesting 
        func_6()

    else:
        func_5()

elif func_7():  # Use elif as there is no further nesting  
    func_9()

else:
    func_8()
© www.soinside.com 2019 - 2024. All rights reserved.