Add comments and move some functions to a class

This commit is contained in:
2020-02-12 12:35:36 +00:00
parent 81ab430b46
commit 58d47d0187

View File

@@ -210,117 +210,135 @@ def prerelease_047822_india_2020():
payments_number = [1, 48, 80] payments_number = [1, 48, 80]
print("%d payment(s) of Rs %.2f" % (payments_number[payment_option - 1], cost / payments_number[payment_option - 1])) print("%d payment(s) of Rs %.2f" % (payments_number[payment_option - 1], cost / payments_number[payment_option - 1]))
def get_input(dict): # 2020 Cambridge iGCSE Computer Science prerelease (UK 1)
tmp = None class prerelease_098421_uk_1_2020:
while (not tmp): def __init__(self):
tmp = input().upper().strip() prerelease_098421_uk_1_2020.run(self)
if (not tmp in dict.keys()):
print("Please enter a valid product code.")
device = None
return tmp
def print_details(dict):
for item in dict.keys():
print("%s: $%.2f, %s" % (item, dict[item][0], dict[item][1]))
def print_chosen(dict, item):
print("%s: %s" % (item, dict[item][1]))
return dict[item][0]
def prerelease_098421_uk_1_2020():
# dictionaries of for items, prices and descriptions
phones_dict = {"BPCM": (29.99, "Compact"),
"BPSH": (49.99, "Clam Shell"),
"RPSH": (199.99, "RoboPhone 5-inch screen and 64 GB memory"),
"RPLL": (499.99,"RoboPhone 6-inch screen and 256 GB memory"),
"YPLS": (549.99,"Y-Phone Standard 6-inch screen and 64 GB memory"),
"YPLL": (649.99, "Y-Phone Deluxe 6-inch screen and 256 GB memory")}
tablets_dict = {"RTMS": (149.99, "RoboTab 8-inch screen and 64 GB memory"),
"RTLM": (299.99, "RoboTab 10-inch screen and 128 GB memory"),
"YTLM": (499.99, "Y-Tab Standard 10-inch screen and 128 GB memory"),
"YTLL": (599.99, "Y-Tab Deluxe 10-inch screen and 256 GB memory")}
# dictionary for all devices (phones and tablets) # retrieve user input & validate it
devices_dict = {**phones_dict , **tablets_dict} def get_input(self, dict):
tmp = None
# keep asking until input is valid
while (not tmp):
# get input, strip spaces and convert to uppercase
tmp = input().upper().strip()
# if input isn't a valid product code
if (not tmp in dict.keys()):
# print error
print("Please enter a valid product code.")
# reset input to blank, so user is asked again
tmp = None
# return input
return tmp
# dictionary for SIMs # print available options
sims_dict = {"SMNO": (0.00, "SIM Free (no SIM card purchased)"), def print_details(self, dict):
"SMPG": (9.99, "Pay As You Go (SIM card purchased)")} # for every item
for item in dict.keys():
# display product code, price and description
print("%s: $%.2f, %s" % (item, dict[item][0], dict[item][1]))
# dictionary for cases # print chosen option from dictionary and return cost
case_dict = {"CSST": (0.00, "Standard"), def print_chosen(self, dict, item):
"CSLX": (50.00, "Luxury")} # print product code and description
print("%s: %s" % (item, dict[item][1]))
# dictionary for chargers # return cost
charger_dict = {"CGCR": (19.99, "Car"), return dict[item][0]
"CGHM": (15.99, "Home")}
# total cost def run(self):
total = 0 # dictionaries of for items, prices and descriptions
# total amount of money saved phones_dict = {"BPCM": (29.99, "Compact"),
saving = 0 "BPSH": (49.99, "Clam Shell"),
# ensure user is asked for input until they don't want more devices "RPSH": (199.99, "RoboPhone 5-inch screen and 64 GB memory"),
run = True "RPLL": (499.99,"RoboPhone 6-inch screen and 256 GB memory"),
# total number of devices ordered "YPLS": (549.99,"Y-Phone Standard 6-inch screen and 64 GB memory"),
devices = 0 "YPLL": (649.99, "Y-Phone Deluxe 6-inch screen and 256 GB memory")}
# run until user doesn't want any more devices tablets_dict = {"RTMS": (149.99, "RoboTab 8-inch screen and 64 GB memory"),
while run: "RTLM": (299.99, "RoboTab 10-inch screen and 128 GB memory"),
# get chosen device "YTLM": (499.99, "Y-Tab Standard 10-inch screen and 128 GB memory"),
print("Choose an option:") "YTLL": (599.99, "Y-Tab Deluxe 10-inch screen and 256 GB memory")}
print_details(devices_dict)
device = get_input(devices_dict)
# increase number of devices ordered
devices += 1
# if phone chosen
if (device in phones_dict.keys()):
# get chosen SIM
print("Choose an option:")
print_details(sims_dict)
sim_payg = get_input(sims_dict)
# get chosen case # dictionary for all devices (phones and tablets)
print("Choose an option:") devices_dict = {**phones_dict , **tablets_dict}
print_details(case_dict)
case = get_input(case_dict)
# get chosen charger(s) # dictionary for SIMs
print("Which charger(s) would you like? Enter the codes for those you want with a space between them, or enter nothing to choose neither") sims_dict = {"SMNO": (0.00, "SIM Free (no SIM card purchased)"),
print_details(charger_dict) "SMPG": (9.99, "Pay As You Go (SIM card purchased)")}
# create array of chosen chargers
chargers = input().upper().split(" ")
# remove invalid product codes
for item in chargers:
if item not in charger_dict:
chargers.remove(item)
# if not first device, apply discount # dictionary for cases
if (devices > 1): case_dict = {"CSST": (0.00, "Standard"),
# add cost of device to total "CSLX": (50.00, "Luxury")}
total += 0.9 * print_chosen(devices_dict, device)
# record discount applied # dictionary for chargers
saving += 0.1 * print_chosen(devices_dict, device) charger_dict = {"CGCR": (19.99, "Car"),
# if first device, don't apply discount "CGHM": (15.99, "Home")}
else:
# add cost of device to total # total cost
total += print_chosen(devices_dict, device) total = 0
# if phone chosen, add cost of SIM to total # total amount of money saved
if (device in phones_dict): saving = 0
total += print_chosen(sims_dict, sim_payg) # ensure user is asked for input until they don't want more devices
# add cost of chosen case to total run = True
total += print_chosen(case_dict, case) # total number of devices ordered
# add cost of chosen chargers to total devices = 0
for charger in chargers:
total += print_chosen(charger_dict, charger) # run until user doesn't want any more devices
# print subtotal while run:
print("Subtotal: $%.2f" % total) # get chosen device
# ask if user want another device, and keep running if they do print("Choose an option:")
run = True if (input("Would you like another device (y/n)?").lower().strip()[0] == "y") else False prerelease_098421_uk_1_2020.print_details(self, devices_dict)
device = prerelease_098421_uk_1_2020.get_input(self, devices_dict)
# print total cost and saving # increase number of devices ordered
print("Total: $%.2f\nSaving: $%.2f" % (total, saving)) devices += 1
# if phone chosen
if (device in phones_dict.keys()):
# get chosen SIM
print("Choose an option:")
prerelease_098421_uk_1_2020.print_details(self, sims_dict)
sim_payg = prerelease_098421_uk_1_2020.get_input(self, sims_dict)
# get chosen case
print("Choose an option:")
prerelease_098421_uk_1_2020.print_details(self, case_dict)
case = prerelease_098421_uk_1_2020.get_input(self, case_dict)
# get chosen charger(s)
print("Which charger(s) would you like? Enter the codes for those you want with a space between them, or enter nothing to choose neither")
prerelease_098421_uk_1_2020.print_details(self, charger_dict)
# create array of chosen chargers
chargers = input().upper().split(" ")
# remove invalid product codes
for item in chargers:
if item not in charger_dict:
chargers.remove(item)
# if not first device, apply discount
if (devices > 1):
# add cost of device to total
total += 0.9 * prerelease_098421_uk_1_2020.print_chosen(self, devices_dict, device)
# record discount applied
saving += 0.1 * prerelease_098421_uk_1_2020.print_chosen(self, devices_dict, device)
# if first device, don't apply discount
else:
# add cost of device to total
total += prerelease_098421_uk_1_2020.print_chosen(self, devices_dict, device)
# if phone chosen, add cost of SIM to total
if (device in phones_dict):
total += prerelease_098421_uk_1_2020.print_chosen(self, sims_dict, sim_payg)
# add cost of chosen case to total
total += prerelease_098421_uk_1_2020.print_chosen(self, case_dict, case)
# add cost of chosen chargers to total
for charger in chargers:
total += prerelease_098421_uk_1_2020.print_chosen(self, charger_dict, charger)
# print subtotal
print("Subtotal: $%.2f" % total)
# ask if user want another device, and keep running if they do
run = True if (input("Would you like another device (y/n)?").lower().strip()[0] == "y") else False
# print total cost and saving
print("Total: $%.2f\nSaving: $%.2f" % (total, saving))
prerelease_098421_uk_1_2020() prerelease_098421_uk_1_2020()