首页 > 解决方案 > 即使表达式在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")

标签: python

解决方案


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

推荐阅读