python - 此源代码中的 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
解决方案
fuel == "diesel" or "gasolin" or "gas"
应该改为,fuel in ("diesel", "gasolin", "gas")
因为目前唯一的比较是燃料和柴油。
推荐阅读
- wordpress - 无法从模态组件添加文本或链接到古腾堡编辑器
- sql-server - 关键字“from”附近、“a”附近、“b”附近的语法不正确。(SQL 服务器)
- python - 按标签分组 csv 的最佳方法文本文件
- mapbox - Mapbox v10 iOS SDK 的条件颜色表达式
- flutter - 如何转换列表
到 GIF? - pandas - 来自 pyarrow 上传数据帧的 BigQuery 架构中不必要的 .list.item 嵌套,列表为列。enable_list_inference 部分有效?
- r - 对多个不同的列求和并在同一个 ggplot 上绘图(不分组变量)
- oracle - 如何在 H2 数据库中创建用户定义的 PIVOT?
- python - PySpark:如何将列表分解为具有顺序命名的多列?
- ios - CoreData: 错误: (778) 数据库 I/O 错误?如何处理这个错误?