gpt4 book ai didi

python - 复合 if elif else 语句 + python

转载 作者:行者123 更新时间:2023-11-30 23:15:04 25 4
gpt4 key购买 nike

我正在尝试从复合/嵌套 if/elif/else 语句中传递一个项目。

if x == 0:
do something
elif x == 1:
do something else
elif x == 2:
if x == 2 and x == whatever:
do something
elif x == 2 and x == whatever:
do something
else:
pass it back out
elif x = 3:
do something
else:
do something

我如何将它从内部 if 传回,以便检查它是否等于 3?

pass 语句在这里有效吗? continue 不断抛出错误。

最佳答案

if x == 0:
do something
elif x == 1:
do something else
elif x == 2 and y == whatever:
do something
elif x == 2 and y == whatever:
do something

elif x = 3:
do something
else:
do something

也许?如果您已经输入了 if/elif 分支,则无法输入新的 if/elif 分支

另一个选择是将其余部分移到里面

if x == 0:
do something
elif x == 1:
do something else
elif x >= 2:
if x == 2 and x == whatever:
do something
elif x == 2 and x == whatever:
do something
elif x = 3:
do something
else:
do something

另一个选项是遵循其他示例并开始一个新的 if block ,尽管您需要小心一点,不要检查其他分支之一可能更改的条件...并且每个选项都必须是互斥的其他选项

关于python - 复合 if elif else 语句 + python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28443164/

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