首页 > 解决方案 > 在 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 语句,以检测它是否正确读取了我的“是”答案,而事实并非如此。

标签: pythonpython-3.xif-statement

解决方案


lower是一个方法,你需要调用它:

if input('Has the graphics card been sold yet?').lower() == "yes":

请注意,正如我所展示的,您不需要在条件本身周围加上括号。


推荐阅读