【问题标题】:If statement overwriting when it shouldn't (Python)如果语句不应该覆盖(Python)
【发布时间】:2013-12-06 03:21:35
【问题描述】:

我不太确定我在这里做错了什么。我将 tTitle 输入为 100,将 tJson 输入为 mm,但是当我应该得到 5 时它会一直返回 6。正如您在“all([])”中看到的那样,我尝试了其他方法,但似乎没有任何效果。

                if "any" or "Any" or "ANY" in tTitle:
                     tCAT = 0
                if "100" in tTitle:
                     tCAT = 1
                if "100" in tTitle and tJson == "tloz2":
                     tCAT = 3
                if "45" in tTitle and tJson == "ss":
                     tCAT = 4
                if all(["100" in tTitle, tJson == "mm"]):
                     tCAT = 5
                if all(["any" or "Any" or "ANY" in tTitle, tJson == "mm"]):
                     tCAT = 6
                else:
                     tCAT = 0

【问题讨论】:

  • 又一个新程序员被布尔逻辑绊倒了。
  • 我觉得我应该把它变成一个字符串
  • if "any" in tTitle.lower(): 也许?

标签: python if-statement int


【解决方案1】:

最后一个if:if all(["any" or "Any" or "ANY" in tTitle, tJson == "mm"]): tCAT = 6实际上是这个意思:

if all([("any") or ("Any") or ("ANY" in tTitle), tJson == "mm"]):
    tCAT = 6

所以它会是真的,因为("any") or ("Any") or ("ANY" in tTitle) 是真的

【讨论】:

  • 我应该用 () so ("ANY") 关闭它 - 如果是这样,它仍然返回错误的 num (6)
  • @user104381​​6,不,我的意思是你的代码是这样的,你的意思应该是"any" in tTitle or "Any" in tTitle or "Any" in tTitle
  • 啊,我没想到会这样,我会记住的,谢谢
【解决方案2】:

通过简单的 tTitle.lower() 修复,然后只要求找到一种类型“任何”谢谢 gnibbler

【讨论】:

    猜你喜欢
    • 2017-01-31
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    相关资源
    最近更新 更多