【发布时间】:2022-01-13 05:22:28
【问题描述】:
在尝试从头开始编写代码后,我试图打印出最高出价者。我已经盯着这个看了一个小时,不明白为什么这一切都有效,但它总是宣布最近的竞标者为中标者,而不是最高的。一双新鲜的眼睛将不胜感激!
#print(logo)
#Then we set variables to store the inputs
print("Welcome to the secret auction program.\n")
#We then create a dictionary to store the functions output
bidders = []
#Function to add bids to dictionary
def create_bidder(name, bid):
bidders.append({
"name": name,
"bid" : bid
})
start_name = input("What is your name?\n")
start_bid = input("What's your bid?\n")
create_bidder(start_name,start_bid)
keep_going = input("Are there any other bidders? Type 'yes' or 'no'\n")
while keep_going == "yes":
clear()
name = input("What is your name?\n")
bid = input("What's your bid?\n")
keep_going = input("Are there any other bidders? Type 'yes' or 'no'\n")
create_bidder(name, bid)
for i in range(0, len(bidders)):
maxBid = -1
maxBidName = "";
bid = int(bidders[i]["bid"])
name = bidders[i]["name"]
if bid > maxBid:
maxBid = bid
maxBidName = name
if keep_going == 'no':
for i in range(0, len(bidders)):
maxBid = -1
maxBidName = "";
bid = int(bidders[i]["bid"])
name = bidders[i]["name"]
if bid > maxBid:
maxBid = bid
maxBidName = name
print(f"Congratulations {maxBidName}! Your bid of ${maxBid} wins!")
if keep_going == 'print':
print(bidders)
【问题讨论】:
-
你正在重置
maxBid = -1循环中的每一次迭代!在循环之前设置它一次。 -
您在每次循环迭代时将 max_bid 设置为 -1,因此每次都将接受任何正值作为新的最高出价
标签: python arrays loops dictionary