Search code examples
pythondebuggingreplitthonny

Bug in Secret Auction Program For Loop


I wrote a program as part of a class I am currently taking and even though I tried fixing this bug using the Thonny debugger, it persists! The code i am working on is in replit.com.

Basically, the bug, when i try running this program on replit, is that it always announces the last candidate as the winner even if the previous candidates submitted a larger bid beforehand which is not the intended result of the program.

In Thonny, I submitted the following code and got the desired result.

bids = {"Alfred": "100", "Cassandra": "20"}
def stuff():
    nominee=0
    for candidate in bids:
        runner=int(bids[candidate])
        if runner>nominee:
            nominee=runner
            winner=candidate
    print(f"The winner is {winner}.")
stuff()

I implemented that line of code in the completed program code below and tried to run it and get the program to work properly and it continues to select the last candidate even if they bid a smaller amount of money and it still did not fix it. Any help would be greatly appreciated!

from replit import clear
from art import logo
newgame=True
while newgame:
    clear()
    print(logo)
    print("Welcome to the secret auction program.")
    ongoing_game=True
    while ongoing_game:
        bids={}
        name=input("What is your name?: ")
        bid=input("What is your bid?: ")
        bids[name]=bid
        continuity=input("Are there any other bidders? Type 'yes' or 'no'. ")
      if continuity=="no":
        ongoing_game=False
    else:
        clear()
    clear()
    def stuff():
      nominee=0
      for candidate in bids:
        runner=int(bids[candidate])
        if runner>nominee:
          nominee=runner
          winner=candidate
          winningbid=bid
        print(f"The winner is {winner} with a bid of ${winningbid}.")
    stuff()
    another=input("Do you want to hold another auction? Type 'yes' or 'no'. ")
    if another=="no":
        newgame=False
else:
    clear
    print("Goodbye!")

Tried using Thonny debugger to debug the issue. The code worked in Thonny, but does not work in the program at large. I apologize if indentation is off somewhere. I tried to edit it for stackoverflow(replit uses 2 spaces automatically and I had to run through manually and add 2 more to each line. Very new to coding.)


Solution

  • To fix this problem, I defined the stuff() function outside of the while loop, so that it could get bids from all rounds.can you please try below code

    from replit import clear
    from art import logo
    
    def find_winner(bids):
      highest_bid = 0
      winner = ""
      for candidate in bids:
        bid = int(bids[candidate])
        if bid > highest_bid:
          highest_bid = bid
          winner = candidate
      return winner, highest_bid
    
    newgame = True
    while newgame:
      clear()
      print(logo)
      print("Welcome to the secret auction program.")
      bids = {}
      ongoing_game = True
      while ongoing_game:
        name = input("What is your name?: ")
        bid = input("What is your bid?: ")
        bids[name] = bid
        continuity = input("Are there any other bidders? Type 'yes' or 'no'. ")
        if continuity == "no":
          ongoing_game = False
      winner, winning_bid = find_winner(bids)
      print(f"The winner is {winner} with a bid of ${winning_bid}.")
      another = input("Do you want to hold another auction? Type 'yes' or 'no'. ")
      if another == "no":
        newgame = False
      else:
        clear()
    
    clear()
    print("Goodbye!")