python-3.x - 处理代码以查看输入的汽车模型是否存在于提供的集合中......但是只有 else 阻止工作
问题描述
即使提供的值在列表中,for 循环仍在工作
尝试在不同的 IDE 中运行代码。但是代码在这些环境中都不起作用
#Check whether the given car is in stock in showroom
carsInShowroom = ["baleno", "swift", "wagonr", "800", "s-cross", "alto", "dezire", "ciaz"]
print("Please enter a car of your choice sir:")
carCustomer = input()
carWanted = carCustomer.lower()
for i in carsInShowroom:
if i is carWanted:
print("Sir we do have the Car")
break
else:
print("Sorry Sir we do not currently have that model")
只有 else 会阻止运行。当我输入时wagonr
,输出显示“对不起先生,我们目前没有那个模型”
解决方案
改变这个,
if i is carWanted: # `is` will return True, if
至
if i == carWanted.strip(): # strip for remove spaces
为什么?
is
是为了参考平等。==
是为了价值平等。
*注意:wagonr
您的输入不 应该是wagon r
推荐阅读
- java - JPA - 如何在 postgres 文本数组中查找字符串列表
- .net - 如何修复“找不到类型或命名空间名称“系统”是否缺少指令或程序集引用 (CS0246)”
- google-chrome - 如何从 Chrome 控制台导出数组
- amp-html - 在 reCAPTCHA 脚本上运行 execute() 时出错
- javascript - Woocommerce:未触发交付方法更改事件处理程序
- linux - Wget 错误:已发送 HTTP 请求,等待响应... 401 Unauthorized Authorization failed
- sql - 将sql查询输出放在变量powershell中
- django - 如何从基于自定义函数的视图中调用基于类的通用 API 视图
- python - Python countlines - 目录和文件
- javascript - 将子组件中的功能获取到父组件中?