pythonloopsdictionaryhighest

Finding the highest key and value in a dictionary


I'm doing a task for a Python course, a 'blind auction', where each user inputs their name and bid into an empty dictionary.

The idea is then that you loop through the dictionary, and find the highest key and value (the 'winner' and 'bid')

Apparently, according to the course, you're supposed to do this using a function, which I don't quite understand, as I would simply do the for loop. I tried to do it, but it won't find the winner.

This is the code: The 'winner_bid' in pycharm says: Local variable 'winner_bid' value is not used

empty_dictionary = {}
game_continue = True

def find_highest_bidder():
    winner_bid = None
    highest_bid = 0
    for key in empty_dictionary:
        if empty_dictionary[key] > highest_bid:
            highest_bid = empty_dictionary[key]
            winner_bid = key

while game_continue:
    name = input("What is your name?\n")
    bid = int(input("What is your price?\n"))
    empty_dictionary[name] = bid

    continue_input = input("Are there any more bidders? Type 'yes' or 'no'\n").lower()
    if continue_input == "no":
        game_continue = False
        find_highest_bidder()
        print(f"The winner is {winner_bid} with a bid of {highest_bid}")
    else:
        print("\n" * 20)

The following is the correct solution for context:

def find_highest_bidder(bidding_record):
    highest_bid = 0
    winner = ""
    for bidder in bidding_record:
        bid_amount = bidding_record[bidder]
        if bid_amount > highest_bid:
            highest_bid = bid_amount
            winner = bidder
    print(f"The winner is {winner} with a bid of ${highest_bid}")


bids = {}
continue_bidding = True
while continue_bidding:
    name = input("What is your name?: ")
    price = int(input("What is your bid?: $"))
    bids[name] = price
    should_continue = input("Are there any other bidders? Type 'yes or 'no'.\n")
    if should_continue == "no":
        continue_bidding = False
        find_highest_bidder(bids)
    elif should_continue == "yes":
        print("\n" * 20)

Solution

  • With the current code, winner_bid is a local variable inside find_highest_bidder that is updated as the loop goes, but you never do anything with it.

    Since you're referencing it in game_continue it seems like you meant to have it as a global variable. That would work, but it's not the best practice. A more robust approach would be to return the entry of the highest bid and entry:

    def find_highest_bidder():
        winner_bid = None
        highest_bid = 0
        for key in empty_dictionary:
            if empty_dictionary[key] > highest_bid:
                highest_bid = empty_dictionary[key]
                winner_bid = key
        return (winner_bid, highest_bid)
    

    and then use them in game_continue:

    winner_bid, highest_bid = find_highest_bidder()
    

    Having said that, you don't really need the find_highest_bidder function. You can use Python's built-in max over the bids' items:

    winner_bid, highest_bid = max(bids.items(), key=lambda x: x[1])