我想制作一个具有多个嵌套 if 的程序,但我找不到在 Python 中执行此操作的可读或好的方法。我附上一张带有我的程序流程的图像;例如,我有很多 if 语句。
我可以实现这个,但是代码很难读。
是否存在以可读的 Python 代码实现此流程图的形式或方法?
这是一个基本练习,旨在了解您是否了解如何使用
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()