Add comments and move some functions to a class
This commit is contained in:
@@ -210,24 +210,42 @@ def prerelease_047822_india_2020():
|
||||
payments_number = [1, 48, 80]
|
||||
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)
|
||||
class prerelease_098421_uk_1_2020:
|
||||
def __init__(self):
|
||||
prerelease_098421_uk_1_2020.run(self)
|
||||
|
||||
# retrieve user input & validate it
|
||||
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.")
|
||||
device = None
|
||||
# reset input to blank, so user is asked again
|
||||
tmp = None
|
||||
# return input
|
||||
return tmp
|
||||
|
||||
def print_details(dict):
|
||||
# print available options
|
||||
def print_details(self, dict):
|
||||
# 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]))
|
||||
|
||||
def print_chosen(dict, item):
|
||||
# print chosen option from dictionary and return cost
|
||||
def print_chosen(self, dict, item):
|
||||
# print product code and description
|
||||
print("%s: %s" % (item, dict[item][1]))
|
||||
# return cost
|
||||
return dict[item][0]
|
||||
|
||||
def prerelease_098421_uk_1_2020():
|
||||
def run(self):
|
||||
# dictionaries of for items, prices and descriptions
|
||||
phones_dict = {"BPCM": (29.99, "Compact"),
|
||||
"BPSH": (49.99, "Clam Shell"),
|
||||
@@ -269,8 +287,8 @@ def prerelease_098421_uk_1_2020():
|
||||
while run:
|
||||
# get chosen device
|
||||
print("Choose an option:")
|
||||
print_details(devices_dict)
|
||||
device = get_input(devices_dict)
|
||||
prerelease_098421_uk_1_2020.print_details(self, devices_dict)
|
||||
device = prerelease_098421_uk_1_2020.get_input(self, devices_dict)
|
||||
# increase number of devices ordered
|
||||
devices += 1
|
||||
|
||||
@@ -278,17 +296,17 @@ def prerelease_098421_uk_1_2020():
|
||||
if (device in phones_dict.keys()):
|
||||
# get chosen SIM
|
||||
print("Choose an option:")
|
||||
print_details(sims_dict)
|
||||
sim_payg = get_input(sims_dict)
|
||||
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:")
|
||||
print_details(case_dict)
|
||||
case = get_input(case_dict)
|
||||
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")
|
||||
print_details(charger_dict)
|
||||
prerelease_098421_uk_1_2020.print_details(self, charger_dict)
|
||||
# create array of chosen chargers
|
||||
chargers = input().upper().split(" ")
|
||||
# remove invalid product codes
|
||||
@@ -299,21 +317,21 @@ def prerelease_098421_uk_1_2020():
|
||||
# if not first device, apply discount
|
||||
if (devices > 1):
|
||||
# add cost of device to total
|
||||
total += 0.9 * print_chosen(devices_dict, device)
|
||||
total += 0.9 * prerelease_098421_uk_1_2020.print_chosen(self, devices_dict, device)
|
||||
# record discount applied
|
||||
saving += 0.1 * print_chosen(devices_dict, device)
|
||||
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 += print_chosen(devices_dict, device)
|
||||
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 += print_chosen(sims_dict, sim_payg)
|
||||
total += prerelease_098421_uk_1_2020.print_chosen(self, sims_dict, sim_payg)
|
||||
# add cost of chosen case to total
|
||||
total += print_chosen(case_dict, case)
|
||||
total += prerelease_098421_uk_1_2020.print_chosen(self, case_dict, case)
|
||||
# add cost of chosen chargers to total
|
||||
for charger in chargers:
|
||||
total += print_chosen(charger_dict, charger)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user