python - 即使表达式在python中为假,while循环仍在迭代
问题描述
我希望循环询问名称和金额,并根据用户的选择是否要计数,如果用户输入'y',则循环应计数,否则如果用户输入'n',则循环应终止,但如果我输入' n' 循环不会停止迭代。
import os
bidders={}
to_countinue=True
while to_countinue:
name=input("Enter your name:\t")
amount=int(input("\nBidding Amount:\t"))
bidders[name]=amount
count=input("\nIs there any other bidders (y/n):\t").lower
if count=="n":
to_countinue=False
os.system('cls')
max=0
won=""
for key in bidders:
if bidders[key]>max:
max=bidders[key]
won=key
os.system('cls')
print(f"The biiding was won by{key} with {max} bidding amount")
解决方案
将lower
简单地为您提供该函数的地址,该地址不太可能等于"n"
。
如果要调用它,则需要使用lower()
,如:
count = input("\nAre there any other bidders (y/n)?:\t").lower()
以下记录显示了差异:
>>> print("Hello".lower)
<built-in method lower of str object at 0x7f4e525fc6f0>
>>> print("Hello".lower())
hello
推荐阅读
- javascript - 在 2 个分隔符之间拆分字符串并包含它们
- python - 图像预处理 - 为 CNN 创建数据集
- c# - 添加小时数与整数 4 位数(小时 1 +小时 2)不带日期时间 C#
- java - 如何使以下分配合法化?
- node.js - sequlize 托管事务块的模拟
- python - 如果方差可用,则计算 pyomo 中的标准偏差
- typescript - 具有推断类型的函数参数在 return 中没有智能感知,而在函数体的其余部分有
- javascript - 我如何删除没有 id 或 somthings 的拥有标签...通过 jquery
- go - 无法使用书中的示例模拟死锁
- node.js - 使用 GET 定义相同的常量,但使用 POST 未定义 - express、mongo、react