【问题标题】:Or comparision of input values或输入值的比较
【发布时间】:2018-12-09 22:16:56
【问题描述】:
compareusr = str(input())

compare = "austin" or "cloud"
if compare == compareusr:
    print("it worked")
else:
    print("it didnt work")

这可能是一个愚蠢的问题。我不是最流利的python,但我认为我对这项比较工作了解的足够多。

有谁知道为什么当 input=cloud 代码不起作用?!?!?它适用于and,但为什么它不适用于or?

【问题讨论】:

标签: python


【解决方案1】:

运算符or 返回逻辑值。在您的代码中,compare 等于 True,因为字符串不为空。将input 转换为str 是多余的。

compareusr = input()

compare = ["austin", "cloud"]
if compareusr in compare:
    print("it worked")
else:
    print("it didnt work")

【讨论】:

  • 实际上,or 在这种情况下充当 elvis 运算符。这意味着compare 等于"austin",因为它不是None 类型。
猜你喜欢
  • 1970-01-01
  • 2015-07-18
  • 2021-03-01
  • 1970-01-01
  • 2019-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多