gpt4 book ai didi

python - 如何在 Python 中实现 "also if"?

转载 作者:太空宇宙 更新时间:2023-11-03 12:12:22 25 4
gpt4 key购买 nike

我想在概念上实现如下内容:

if condition1:
action1()
also if condition2:
action2()
also if condition3:
action3()
also if condition4:
action4()
also if condition5:
action5()
also if condition6:
action6()
else:
print("None of the conditions was met.")

什么是实现这种逻辑的合理而清晰的方法? else 如何绑定(bind)到多个 if 语句?我会被迫创建一个 bool 值来跟踪事物吗?

最佳答案

我建议:

if condition1:
action1()
if condition2:
action2()
...
if not any(condition1, condition2, ...):
print(...)

关于python - 如何在 Python 中实现 "also if"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24494596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com