首页 > 解决方案 > 此源代码中的 if 语句有什么问题?

问题描述

fuel = input()
liters = float(input())

if liters >= 25:
    if fuel == "diesel" or "gasolin" or "gas":
        print(f"You have enough {fuel}.")
    else:
        print("Invalid fuel!") # Why that not work

elif liters < 25:
    if fuel == "diesel" or "gasolin" or "gas":
        print(f"Fill your tank with {fuel}!")
    else:
        print("Invalid fuel!") # Why that not work

else:
    print("Invalid fuel!") # Why that not work

标签: pythonif-statement

解决方案


fuel == "diesel" or "gasolin" or "gas"应该改为,fuel in ("diesel", "gasolin", "gas")因为目前唯一的比较是燃料和柴油。


推荐阅读