-1

My goal is to write code that allows people to place blind bids and then display the winner from the max value of the dictionary at the end. It works perfectly within inputs of the same digits (ex. 10 and 50, 100 and 500) but decides that 500 is the winner vs 1000, or 5000 is the winner against 11000. Im not sure why it decides the lower value is greater. I have tested a multitude of inputs and bids and it seems that each time you add another digit to the input it bugs.


def cls():
    os.system('cls' if os.name=='nt' else 'clear')
bid_dict = {}

def winning(): 
    while True:
        winner = max(bid_dict, key=bid_dict.get)
        price = max(bid_dict.values())      
        print(f'The winner is {winner} with a bid of ${price}')
        print(bid_dict)
        break
while True:
    name = input("What is your name? ")
    bid = input('What is your bid? ')
    bid_dict[name] = bid
    more = (input("Are there more bidders? ")).lower()
    if more == "yes":
        cls()
        continue
    else:
        cls()
        winning()
  • To debug this, have you tried inspecting `bid`? Try `print(bid)` and `print(type(bid))` to see what you're working with. – CrazyChucky Aug 25 '22 at 00:08

1 Answers1

1

By default, input() returns a string. So when you compare the values using max(), it sorts them as strings and returns the string that starts with the highest value (i.e. the string that starts with the largest number).

You need to save the bids as some sort of number; either an int or a float would work, depending on whether you want to allow decimal places. That will allow you to compare them as actual numbers.

Da Chucky
  • 781
  • 3
  • 13