Home > OS >  My code keeps iterating at the while loop
My code keeps iterating at the while loop

Time:12-13

Please im building a simple a bid auction application using pytho.

however it kept iterating a the while loop

meawhile there is another condition after the while loop.

Please help.

Thank you

import os


print ("Welcome To The Bid Auction")
another_bid='yes'

#create an empty dict to store all bids
bids={}

while another_bid=="yes":

   bidder_name=input ( str("Enter Bidder Name:"))

   bid_price= float(input("Enter Bid Amount"))
 
#add input to dict   

bids[bidder_name]= bid_price 



#clear screen for another input


another_bid= input (" Is there another bidder? Yes or No__:").lower()
if another_bid=='yes':
    os.system("cls")
    bidder_name=input ("Enter Bidder Name:")

    bid_price= float(input("Enter Bid Amount"))



#  getting the highest bid
max_bid=max(bids.values())
print(f"max bid amount is:""{max_bid}")
highest_bidders =[key for key, value in bids.items() if value==max_bid]
highest_bidder="".join(highest_bidders)
print (f'string: {highest_bidder}')
print (f'Thank you all for participating, the winner is {highest_bidder}')

CodePudding user response:

In the while loop while another_bid=="yes":, there is no statement that could change another_bid to false. While loops go forever if the condition is always true. I would suggest adding:

if (bidder_name == "end"):
     another_bid = "no"

to the end of the while loop. You can change "end" to something else, but the user needs a way to tell the program to stop looping.

CodePudding user response:

You're not updating the another_bid, to solve this move code below inside the while block, and break the while loop when the another_bid equals to no.

another_bid= input (" Is there another bidder? Yes or No__:").lower()
if another_bid=='yes':
    os.system("cls")
    bidder_name=input ("Enter Bidder Name:")
    bid_price= float(input("Enter Bid Amount"))

Also there is no need to take input again, and you can also remove the str method inside input function.

while another_bid=="yes":
   bidder_name = input("Enter Bidder Name:")
   bid_price = float(input("Enter Bid Amount"))

   bids[bidder_name]= bid_price 

   another_bid= input ("Is there another bidder? Yes or No__:").lower()
   if another_bid=='yes':
      os.system("cls")
   else:
       break
  • Related