Warning! It seems that you are using Dodona within another webpage, so not everything may work properly. Let your teacher know so that he can solve the problem by adjusting a setting in the learning environment. In the meantime, you can click this link to open Dodona in a new window.
Auction System
Sign in to test your solution.
class Lot:
"""
Represents an item (or set of items) in an auction: a lot.
Attributes:
- number (int): A unique identifying number for the lot.
- description (str): A description of the lot.
- highest_bid (Bid): The current highest bid for this lot.
"""
def __init__(self, number, description):
"""
Construct a Lot, setting its number and description.
Parameters:
- number (int): The lot number.
- description (str): A description of this lot.
"""
self.number = number
self.description = description
self.highest_bid = None
def bid_for(self, bid):
"""
Attempt to bid for this lot. A successful bid
must have a value higher than any existing bid.
Parameters:
- bid (Bid): A new bid.
Returns:
- bool: True if the bid is successful, False otherwise.
"""
if self.highest_bid is None:
# There is no previous bid.
self.highest_bid = bid
return True
elif bid.value > self.highest_bid.value:
# The bid is better than the previous one.
self.highest_bid = bid
return True
else:
# The bid is not better.
return False
def __str__(self):
"""
Return a string representation of this lot's details.
Returns:
- str: A string representation of this lot.
"""
details = f"{self.number}: {self.description}"
if self.highest_bid is not None:
details += f" Bid: {self.highest_bid.value}"
else:
details += " (No bid)"
return details
def get_number(self):
"""
Return the lot's number.
Returns:
- int: The lot's number.
"""
return self.number
def get_description(self):
"""
Return the lot's description.
Returns:
- str: The lot's description.
"""
return self.description
def get_highest_bid(self):
"""
Return the highest bid for this lot.
This could be None if there is no current bid.
Returns:
- Bid or None: The highest bid for this lot, or None if there is no current bid.
"""
return self.highest_bid
class Bid:
"""
Represents an auction bid.
It contains a reference to the Person bidding and the amount bid.
Attributes:
- bidder (Person): The person making the bid.
- value (int): The value of the bid.
"""
def __init__(self, bidder, value):
"""
Create a bid.
Parameters:
- bidder (Person): Who is bidding for the lot.
- value (int): The value of the bid.
"""
self.bidder = bidder
self.value = value
def get_bidder(self):
"""
Get the bidder.
Returns:
- Person: The bidder.
"""
return self.bidder
def get_value(self):
"""
Get the value of the bid.
Returns:
- int: The value of the bid.
"""
return self.value
class Person:
"""
Represents a person who participates in an auction.
Attributes:
- name (str): The name of the person.
"""
def __init__(self, name):
"""
Create a new person with the given name.
Parameters:
- name (str): The person's name.
"""
self.name = name
def get_name(self):
"""
Get the person's name.
Returns:
- str: The person's name.
"""
return self.name
class Auction:
"""
Represents an auction.
Attributes:
- lots (list): The list of Lots in this auction.
- next_lot_number (int): The number that will be given to the next lot entered into this auction.
"""
def __init__(self):
"""
Create a new auction.
"""
self.lots = []
self.next_lot_number = 1
def enter_lot(self, description):
"""
Enter a new lot into the auction.
Parameters:
- description (str): A description of the lot.
"""
self.lots.append(Lot(self.next_lot_number, description))
self.next_lot_number += 1
def show_lots(self):
"""
Show the full list of lots in this auction.
"""
for lot in self.lots:
print(lot.__str__())
def make_a_bid(self, lot_number, bidder, value):
"""
Make a bid for a lot. A message is printed indicating whether the bid is successful or not.
Parameters:
- lot_number (int): The lot being bid for.
- bidder (Person): The person bidding for the lot.
- value (int): The value of the bid.
"""
selected_lot = self.get_lot(lot_number)
if selected_lot is not None:
bid = Bid(bidder, value)
successful = selected_lot.bid_for(bid)
if successful:
print("The bid for lot number", lot_number, "was successful.")
else:
highest_bid = selected_lot.get_highest_bid()
print("Lot number:", lot_number, "already has a bid of:", highest_bid.get_value())
def get_lot(self, lot_number):
"""
Return the lot with the given number. Return None if a lot with this number does not exist.
Parameters:
- lot_number (int): The number of the lot to return.
Returns:
- Lot or None: The lot with the given number, or None if it does not exist.
"""
for lot in self.lots:
if lot.get_number() == lot_number:
return lot
print("Lot number:", lot_number, "does not exist.")
return None
You can submit as many times as you like. Only your latest submission will be taken into account.
Sign in to test your solution.
Python sandbox
This window allows you to run Python code without installing a thing. The code you write here is not automatically submitted to Dodona. This feature is still in full development, so we're happy to hear about your suggestions and issues using the contact form.