是否有一种方法可以找出哪种条件(在许多情况下使用或使用)在“ if”中由于它在IF内部输入,因此被评估为true吗?

发布于 2025-02-09 00:19:52 字数 247 浏览 3 评论 0原文

我有一个if语句,假设:

if cond1 or cond2 or cond3:  
     do_something()

我可以找出这三个条件(cond1,cond2和cond3)是正确的,因为它进入了内部并执行do_something()?

PS:我正在寻找答案,这并不建议我使用另外2个如果在我的下方以找出正确的答案。

I have an if statement, let's say:

if cond1 or cond2 or cond3:  
     do_something()

Can I find out which among these three conditions (cond1,cond2 and cond3) was true because of which it entered inside and executed do_something()?

P.S: I'm looking for answers which doesn't suggest me to use another 2 if's below my if to find out which was true.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您可以在循环中进行操作:

for i, j in enumerate((cond1, cond2, cond3)):
    if j:
        do_something()

        print(i)  #Do whatever you want to do with the 1st condition that was true
        break

You could do it in a for loop:

for i, j in enumerate((cond1, cond2, cond3)):
    if j:
        do_something()

        print(i)  #Do whatever you want to do with the 1st condition that was true
        break
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文