英文:
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.)
英文:
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.)
答案1
得分: 0
以下是您提供的代码的翻译部分:
要解决这个问题,我将stuff()函数定义在while循环外部,以便它可以获取所有回合的出价。请尝试下面的代码
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("欢迎来到秘密拍卖程序。")
bids = {}
ongoing_game = True
while ongoing_game:
name = input("你叫什么名字?:")
bid = input("你的出价是多少?:")
bids[name] = bid
continuity = input("还有其他竞拍者吗?请输入 'yes' 或 'no':")
if continuity == "no":
ongoing_game = False
winner, winning_bid = find_winner(bids)
print(f"获胜者是 {winner},出价为 ${winning_bid}。")
another = input("你想再举行一次拍卖吗?请输入 'yes' 或 'no':")
if another == "no":
newgame = False
else:
clear()
clear()
print("再见!")
请注意,我已经将HTML实体(例如"
)替换为正常的引号字符,以确保代码的可读性。
英文:
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!")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论