python - 在 if 语句中使用 Python 输入时无法正常工作
问题描述
我制定了一个快速预算程序来确定购买东西需要多长时间,但我遇到了一个问题。每当我尝试运行它时,一次性销售项目都不会添加到余额中。我的代码如下:
graphicsCard = 50
amdCpu = 80
itelCpu = 99
ram = 140
guitar = 199
case = 99
balance = 21
if (input('Has the graphics card been sold yet?').lower == "yes"):
balance += graphicsCard
if (input('Has the AMD cpu been sold yet?').lower == "yes"):
balance += amdCpu
if (input('Has the intel CPU been sold yet?').lower == "yes"):
balance += intelCpu
if (input('Has the RAM been sold yet?').lower == "yes"):
balance += ram
if (input('Has the Guitar been sold yet?').lower == "yes"):
balance += guitar
if (input('Has the Case been sold yet?').lower == "yes"):
balance += case
我在第一个 if 之后添加了一个 else 语句,以检测它是否正确读取了我的“是”答案,而事实并非如此。
解决方案
lower
是一个方法,你需要调用它:
if input('Has the graphics card been sold yet?').lower() == "yes":
请注意,正如我所展示的,您不需要在条件本身周围加上括号。
推荐阅读
- api - Elixir:在大型结构化数据中提取元素
- python - Python PYSFTP - 将私钥作为字符串/文本传递,而不是传递文件路径
- javascript - React-Native 改变 inputText 背景一秒钟
- c++ - 有人可以解释为什么lower_bound 会这样工作吗?
- angular - Angular RxJs:请求数组中的每个项目
- python - reStructuredText 在 return 语句上带有换行符
- c# - 比较两个列表并从另一个列表中删除包含条目的每个项目
- c# - 如何从.net动态数组对象中获取属性值
- applescript - 如何获取应用程序的属性
- java - 使用 @EmbeddedId 请求休眠不工作(Spring boot - 2.3.1.RELEASE)