Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
m = 5 if m == 1 or 4: print("x") else: print("y")
我希望这段代码打印 y,而不是 x。
4评估为真,因此结果
4
m = 5 if m == 1 or m == 4: print("x") else: print("y")
小提琴
零以外的数字是隐含的 True。阅读运算符优先级
你写的实际上翻译成:
m = 5 if (m == 1) or True: print("x") else: print("y")
你的if永远是真实的。
if
尝试:
if m == 1 or m == 4:
反而。