Skip to main content
 首页 » 编程设计

python之如何简化 python 中的 if 条件

2025年05月04日9txw1958

我想知道是否有更简单的方法来编写 if 语句的条件。类似于 item1, item2 == "wood":。我目前有这段代码:

item1 = input("item1: ") 
item2 = input("item2: ") 
 
if item1 == "wood" and item2 == "wood": 
    print("You created a stick") 

请您参考如下方法:

这不是一笔巨大的节省,但您可以取出“木头”和

item1 = input("item1: ") 
item2 = input("item2: ") 
 
if item1 == item2 == "wood": 
    print("You created a stick")