Skip to content

Instantly share code, notes, and snippets.

@gsinclair
Created May 14, 2019 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsinclair/904c42180c1eca86ac058a29639c262f to your computer and use it in GitHub Desktop.
Save gsinclair/904c42180c1eca86ac058a29639c262f to your computer and use it in GitHub Desktop.
Pre-release code for the auction thingy
### Initialisation
### - Input number of items
### - Set up all arrays for all tasks
### - item numbers
### - description
### - reserve
### - number of bids
### - highest bid
TESTING = False
if TESTING:
N = 4
ItemNumber = [0, 5361, 1359, 4141, 1901]
Description = ["", "Car", "Sofa", "Book", "Television"]
Reserve = [0.0, 4500.00, 142.50, 8.75, 399.00]
NumBids = [0, 3, 0, 13, 5]
HighBid = [0.0, 2210.00, 0.00, 19.50, 380.00]
else:
N = int(input("How many items in this auction? "))
ItemNumber = [0] * (N+1)
Description = [""] * (N+1)
Reserve = [0.0] * (N+1)
NumBids = [0] * (N+1)
HighBid = [0.0] * (N+1)
### Task 1
### - Enter each item number, description, reserve price.
### - function to set index i
### - return true if set, false if invalid
# We prompt the user for item number, description, reserve price.
# If valid, we _set_ the arrays index i, and return True.
# If invalid, we print an error message and return False.
def input_item(i):
print("")
print("Accepting information for item", i, "of", N)
item_num = int(input(" * Item number: "))
if item_num <= 0:
print("---------- Error: item number must be positive")
return False
if item_num in ItemNumber:
print("---------- Error: item number already used")
return False
description = input(" * Description: ")
if description == "":
print("---------- Error: description can't be empty")
return False
reserve = float(input(" * Reserve: "))
if reserve <= 0:
print("---------- Error: reserve must be positive")
return False
# If we get this far, there was no error.
# Store the data collected in the arrays.
ItemNumber[i] = item_num
Description[i] = description
Reserve[i] = reserve
NumBids[i] = 0
HighBid[i] = 0.0
return True
def task1():
i = 1
while i <= N:
if input_item(i) == True:
# Item was valid, so go on to next item.
i = i + 1
else:
# Item was invalid, so do this item again.
pass
### Task 2
### - Press V to view items or B to bid or Q to quit
### - if V then view_items()
### - just display summary of all items
### - if B then bid()
### - enter buyer number (which we ignore)
### - enter item number
### - enter bid amount
### - record bid if it's high enough; reject otherwise
def view_items():
print("")
print("Items on sale:")
print("")
for i in range(1,N+1):
print(" Item:", ItemNumber[i], " Bid:", HighBid[i], " ", Description[i])
# Return True if a valid bid was made; False otherwise.
def bid():
print("Enter the following information to make a bid:")
buyer_num = int(input(" * Buyer number: "))
if buyer_num < 0:
print("---------- Error: buyer number must be positive")
return False
item_num = int(input(" * Item number: "))
try:
item_idx = ItemNumber.index(item_num)
except ValueError:
print("---------- Error: item number", item_num, "does not exist")
return False
bid = float(input(" * Bid ($): "))
if bid <= HighBid[item_idx]:
print("---------- Error: bid too low")
return False
# If we get this far, we have a valid bid.
# Update the arrays.
NumBids[item_idx] = NumBids[item_idx] + 1
HighBid[item_idx] = bid
print("Thank you; your bid has been entered.")
return True
def task2():
while True:
print("")
print("[V] View items [B] Bid [Q] Quit")
choice = input("> ")
if choice == "V":
view_items()
elif choice == "B":
bid()
elif choice == "Q":
print("Thanks for all your bids.")
return
else:
print("Invalid choice; try again.")
### Task 3
### - calculate and display total fee from sold items
### - display item # and final bid for unsold items
### - display item # of items that received no bids
### - display the count of each of the above three categories
### * variables: num_sold, num_unsold, num_unbid, total_fee
### * run through items three times to display info and update counts
### * display totals
def task3():
total_fee = 0.0
num_sold = 0
num_unsold = 0
num_unbid = 0
print("")
for i in range(1,N+1):
if HighBid[i] >= Reserve[i]:
# It sold.
num_sold = num_sold + 1
total_fee = total_fee + (0.1 * HighBid[i])
print("Number of items sold:", num_sold)
print("Total fee ($): ", total_fee)
print("")
print("Unsold items (and highest bid):")
for i in range(1,N+1):
if HighBid[i] < Reserve[i] and NumBids[i] > 0:
print(" ", ItemNumber[i], "($" + str(HighBid[i]) + ")")
num_unsold = num_unsold + 1
print("")
print("Items with no bids:")
for i in range(1,N+1):
if NumBids[i] == 0:
print(" ", ItemNumber[i])
num_unbid = num_unbid + 1
print("")
print("Summary")
print(" Items sold: ", num_sold)
print(" Items unsold:", num_unsold)
print(" Items unbid: ", num_unbid)
task1()
task2()
task3()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment