0

I am new to Python. I am learning through this 100 days video tutorial from Udemy. However surprisingly same code worked for the tutor on video but not for me. After going through current posts with similar error I couldn't figure out the solution and decided to post my question. I have checked in Thonny debugger an code- bid_amnt = bidding_record[bidder] is where value comes with the coma at the end. I have tried converting it to integer by int(bidding_record[bidder]) also but it didn't work. Please tell me how to correct.

#Code snippet
def find_highest_bidder(bidding_record):
    #bidding_record = {Angela:123, James:321} examlpe of dictionary prepared
    highest_bid = 0
    winner = ""
    for bidder in bidding_record:   
        bid_amnt = bidding_record[bidder]
        if bid_amnt > highest_bid:
            highest_bid = bid_amnt
            winner = bidder
  print(f"Winner of this bid is {winner} with ${highest_bid}")

#Declaring variable for loop and empty dictionary
bid_on = True

while bid_on == True:
    #Taking user Inputs
    name = input("Please enter your name: \n")
    bid_value = int(input("please enter your bid amount: $"))
    other_users = input("are there other users who want to bid type yes/no:  \n").lower() 
    bid_dict[name] = bid_value,
  
    #Calling function by passing dictionary in argument  
    find_highest_bidder(bid_dict)
  
    #Calling clear function from replit if there are no more users  
    if other_users == "no":
        bid_on = False
    else:
    clear()
  • 2
    udemy videos also has Q&A, what was the response there from instructor? here in SO, a minimal reproducible example would help someone to provide the answer – Naveed Sep 26 '22 at 14:21
  • 2
    Where do you define `bid_dict`? Also which line is throwing the error? You should be able to see where the error is occurring to debug it. – abinitio Sep 26 '22 at 14:21
  • 1
    `bid_dict[name] = bid_value,` assigns a tuple as your dictionary value. Get rid of that comma. – jasonharper Sep 26 '22 at 14:24
  • Based on the shown code, the trailing comma at `bid_dict[name] = bid_value,` makes no sense and causes the problem. – Michael Butscher Sep 26 '22 at 14:24
  • I have checked in Thonny debugger an code: bid_amnt = bidding_record[bidder] is where it throws error, I have not posted it on Udemy I will do there as well. However I hope to get help here please. – Anurag Jain Sep 26 '22 at 14:26
  • Changing `bid_dict[name] = bid_value,` to `bid_dict[name] = bid_value` will make your code work fine – codester_09 Sep 26 '22 at 14:28
  • Hi codester09 the change you have suggested are same as previous I guessed something left off – Anurag Jain Sep 26 '22 at 14:37
  • Hi Michael Butscher, You are spot on, even my opinion is same but just don't know how to correct it. I am also not aware of Tuples or how to add it in dictionary so going through it but at this moment I am looking for how to correct my code. – Anurag Jain Sep 26 '22 at 14:41
  • Hey Guys please tell me how can I remove trailing comma here, I tried using rstrip() but didnt work and I got following mesage- bid_amnt = bid_amnt.rstrip(',') AttributeError: 'tuple' object has no attribute 'rstrip' – Anurag Jain Sep 26 '22 at 14:52

1 Answers1

1

I see what happened. In the line bid_dict[name] = bid_value, you are defining bid_dict[name] to be a tuple since bid_value has a , after it.

In python, whenever you put a comma after a variable like this, it expects it to be a tuple and converts it to that datatype.

Then in the line if bid_amnt > highest_bid: it is trying to compare bid_amnt which is the tuple you defined above with bid_value, to the int you defined as highest_bid = 0

Remove that comma and it should work.

abinitio
  • 96
  • 5
  • Hello Abinitio, Please guide me how to achieve it I tried rstrip() but it says bid_amnt = bid_amnt.rstrip(',') AttributeError: 'tuple' object has no attribute 'rstrip' – Anurag Jain Sep 26 '22 at 14:50
  • 1
    Just erase the comma from the code. Instead of writing `bid_dict[name] = bid_value,` just write `bid_dict[name] = bid_value` – abinitio Sep 26 '22 at 15:03
  • Thanks a tons abinitio, it was so stupid of me and python was not giving me proper reply. I owe you one. – Anurag Jain Sep 26 '22 at 16:46
  • No problem. Here's a question that addresses this problem: https://stackoverflow.com/questions/3750632/why-does-adding-a-trailing-comma-after-an-expression-create-a-tuple – abinitio Sep 26 '22 at 16:53