Solution for I tried to convert str into an int, but I still can’t get the number I need for comparison
is Given Below:
I tried to convert str
into an int
,
but I still can’t get the number I need for comparison.
I am referring to the very first function – highest_bid()
on the line
old_player = bidder_list["Bid"]
Full code:
from replit import clear
bidder_list = []
bidding_finished = True
def highest_bid(bidder_list):
for bidder in bidder_list:
new_player = 0
old_player = bidder_list["Bid"]
old_player = int(old_player)
if new_player < old_player:
new_player = old_player
print(bidder_list)
print(new_player)
while bidding_finished:
bidder = input("What is your name? n")
bid = input("How much are you willing to pay? n$")
continue_bid = input("Are you the last bidder? Yes or No? n").lower()
def auction(bidder, bid):
bidder_list.append({"Bidder": bidder, "Bid": bid})
auction(bidder = bidder, bid = bid)
clear()
if continue_bid == "No" or continue_bid == "n":
bidding_finished = False
highest_bid(bidder_list)
print(bidder_list)
print("Bidding Finished! The Winner will always be Percy.")
Have you tried changing
auction(bidder = bidder, bid = bid)
to be
auction(bidder = bidder, bid = int(bid))
You have to change continue_bid == "No"
to continue_bid == "no"
since you convert the input to all lowercase.
When you cast old_player to an integer here:
old_player = bidder_list["Bid"]
old_player = int(old_player)
is fine.
However,
def auction(bidder, bid):
bidder_list.append({"Bidder": bidder, "Bid": bid})
Here, you are creating a list that contains a dictionary. You can’t index the list by doing
old_player = bidder_list["Bid"]
You need to dereference the list and then go to the dictionary like:
old_player = bidder_list[0]["Bid"]
Since you are iterating over the entire bidder_list as bidder for each index,
you can do this instead:
def highest_bid(bidder_list):
for bidder in bidder_list:
new_player = 0
old_player = bidder["Bid"]
old_player = int(old_player)
print(f'value: {old_player} type: {type(old_player)}')
if new_player < old_player:
new_player = old_player
print(bidder_list)
print(new_player)