Click here to Skip to main content
16,019,018 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Python
highest_bid = 0
    winner = None
    for bidder in bid_log:
        bid_amount = bidder["bid"]

        if bid_amount > highest_bid:
            highest_bid = bid_amount
            winner = bidder
    print(f"The winner is {winner['name']}")


What I have tried:

I tried looking up how it's done, but it was very complicated. So, I hope someone here can help.
Posted
Updated 27-Oct-23 5:24am
v3
Comments
Hamza Ayub 26-Oct-23 7:48am    
the code looks like this because of the editor in this website
. so dont mind that


1 solution

An heavily commented copy of the above code:
Python
highest_bid = 0 ##  assuming bids are greater than 0, this is a good initialization value
winner = None   ## another sensible initialization value
for bidder in bid_log: ## this is a loop over the items of the list, every item is a dictionary, e.g. { "Name" : "Bob", "bid" : 100 }

  bid_amount = bidder["bid"]  ## retrieve the bid from the current dictionary

  if bid_amount > highest_bid: ## if the bid is greater than the (current) maximum then...
    highest_bid = bid_amount  ## update the current maximum bid value
    winner = bidder ## keep track of the relative bidder

print(f"The winner is {winner['name']}") ## eventually output the result
 
Share this answer
 
v2
Comments
Hamza Ayub 26-Oct-23 8:05am    
thanks, that simplifies it in a very simple manner.

Thank you
CPallini 26-Oct-23 8:31am    
You are welcome.
Maciej Los 27-Oct-23 11:28am    
5ed!
CPallini 27-Oct-23 12:38pm    
Thank you!
Maxim Kartavenkov 27-Oct-23 13:48pm    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900