0

I started 100 days of code and this project is a silent auction (day 9). You take a user's name and bid price, store it in a dictionary, and then clear it (prompting to see if there are other users). The whole point is to collect the keys/values (that are unknown) and sort them so that it prints the highest value (bidder).

Since I don't know what the keys or values in the dictionary would be I wanted to sort the dictionary after the "No" prompt. Then I decided to convert the dictionary to a list so I can access the keys and values separately. The reason I needed to do this was because in the print statement I print the name (key) and bid price (value) in different parts of the sentence.

Example inputs:

{'alan': '115', 'jay': '125', 'alex': '155'} ['alan', 'jay', 'alex'] ['115', '125', '155']

Output: The winner is alex with a bid of $155.

I run into an error though when the user enters $5 as an input:

{'john': '105', 'alex': '115', 'jae': '5'} ['john', 'alex', 'jae'] ['105', '115', '5']

The winner is jae with a bid of $5.

Also another error "Local variable referenced before assignment" in regard to key_list and key_value. The solution I read was to write global at the beginning of the function.

    from replit import clear
    from operator import itemgetter
    #HINT: You can call clear() to clear the output in the console.
    from art import logo 
    print(logo)
    
    bid_dictionary = {}
    
    def main():
        global key_list
        global value_list
        name = input("What is your name? ")
        bid_price = input("What is your bid price? ")
    
        bid_dictionary[name] = bid_price
    
        other_users = input("Are there other users who want to bid? Enter Yes or No. ").lower()
        if other_users == "yes":
            clear()
            main()
        elif other_users == "no": 
            clear()
            list_sorted = dict(sorted(bid_dictionary.items(), key=lambda item: item[1]))
            key_list = list(list_sorted.keys())
            value_list = list(list_sorted.values())
          
            print(list_sorted, key_list, value_list)
        
            print(f"The winner is {key_list[-1]} with a bid of ${value_list[-1]}.")      
    
    main()
                      
Mechanic Pig
  • 6,756
  • 3
  • 10
  • 31
Clearfall
  • 11
  • 1
  • I would avoid a recursive call. Check https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – Ignatius Reilly Sep 04 '22 at 15:42
  • Are you sure you want to call *main()* recursively? Would it help if the bid price was a number (float/int) instead of a string? What happens if two (or more) bidders enter the same bid price? – DarkKnight Sep 04 '22 at 15:43

1 Answers1

3

list_sorted = dict(sorted(bid_dictionary.items(), key=lambda item: item[1]))

you are sorting prices as strings here (item[1] is a string). "5" comes last in terms of dictionary order (how strings are compared).

you'll need to convert the price into integers before comparing

try this:

list_sorted = dict(sorted(bid_dictionary.items(), key=lambda item: int(item[1])))
ACA
  • 151
  • 6
  • 1
    I would consider converting the prince to number at input time. So, if later you decide you want to do more stuff with the price, is not necessary to convert it again every time. – Ignatius Reilly Sep 04 '22 at 16:13
  • Thank you @IgnatiusReilly! I accepted the answer. I'll take a look at your feedback about a recursive call. – Clearfall Sep 04 '22 at 18:09