This repository has been archived on 2025-11-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
small-projects/computer-science-igcse-prereleases.py

344 lines
16 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# specimen Cambridge iGCSE Computer Science prerelease
def prerelease_047802_specimen():
# define lists for all required data
student_names = []
test_1_marks = []
test_2_marks = []
test_3_marks = []
total_marks = []
# define number of students to record data for
number_of_students = 30
# define variable for storing total marks gained by all students overall
sum = 0
# function for prompting user for input, then checking it against conditions
def getMark(test_number, student_index, max_mark):
# keep asking for the mark until a valid mark is entered
mark = None
while not mark:
# ask for mark of given student, for given test number
mark = input("%s test %d mark: " % (student_names[student_index], test_number))
# check if mark is given and that it's numeric
if (mark and mark.isnumeric()):
# check mark is in acceptable range of values
if (int(mark) < 0 or int(mark) > max_mark):
mark = None
else:
mark = None
# if input is invalid, print error message
if not mark:
print("Invalid input. Please enter a number between 0 and %d." % max_mark)
# return mark
return int(mark)
# do this for all students
for i in range(number_of_students):
# retrieve and store student's name
student_names.append(input("Student %d name: " % (i + 1)))
# do this for all students
for i in range(number_of_students):
# retrieve and store student's test marks
# test #1 has a max mark of 20
test_1_marks.append(getMark(1, i, 20))
# test #2 has a max mark of 25
test_2_marks.append(getMark(2, i, 25))
# test #3 has a max mark of 35
test_3_marks.append(getMark(3, i, 35))
# record student's total marks
total_marks.append(test_1_marks[i] + test_2_marks[i] + test_3_marks[i])
# add student's total marks to sum of all marks gained by all students overall
sum += total_marks[-1]
# print each student's total marks
for i in range(number_of_students):
print("%s: %d" % (student_names[i], total_marks[i]))
# print average total for all students
print("Average total: %.2f" % (sum / 30))
# 2020 Cambridge iGCSE Computer Science prerelease (India)
def prerelease_047822_india_2020():
# ask which base model user wants
print("Which model?")
print("(1) Hackback - Rs 5.35 Lakh")
print("(2) Saloon - Rs 4.95 Lakh")
print("(3) Estate - Rs 6.25 Lakh")
# keep asking until valid input is entered
model = None
while not model:
# ask for choice
model = input()
# if input isn't a number, display error
if (not model.isnumeric()):
model = print("Invalid input. Please enter a number between 1 and 3.")
model = None
# if input is a number outside valid range, display error
elif (int(model) < 1 or int(model) > 3):
model = print("Invalid input. Please enter a number between 1 and 3.")
model = None
# get cost of chosen base model
model_costs = [535000, 495000, 625000]
cost = model_costs[int(model) - 1]
# ask which optional extras user wants
print("Optional extras:")
print("(1) Set of luxury seats - Rs 45000")
print("(2) Satellite navigation - Rs 5500")
print("(3) Parking sensors - Rs 10000")
print("(4) Bluetooth connectivity - Rs 350")
print("(5) Sound system - Rs 1000")
# ask for input, remove duplicated characters and remove non-numerical characters
optional_extras = "".join(char for char in "".join(set(input("Please enter the numbers of the optional extras you would like. Invalid options will be ignored: "))) if char.isdigit())
# remove numbers outside of valid range
optional_extras = "".join(char for char in optional_extras if 0 < int(char) < 6)
# add costs of chosen optional extras to total cost
optional_extra_costs = [45000, 5500, 1000, 350, 1000]
optional_extras_cost = 0
for number in optional_extras:
cost += optional_extra_costs[int(number) - 1]
optional_extras_cost += optional_extra_costs[int(number) - 1]
# ask if user is an existing customer
existing_customer = input("Are you an existing customer (y/n)? ").lower()
# evaluate input to boolean
existing_customer = True if (existing_customer[0] == "y" or existing_customer[-1] == "s") else False
# ask if user has car to trade in
car_trade = input("Do you have a car to trade in (y/n)? ").lower()
# evaluate input to boolean
if (car_trade[0] == "y" or car_trade[-1] == "s"):
car_trade = True
# keep asking for trade-in amount until valid input entered
trade_amount = None
while (not trade_amount):
# ask for input
trade_amount = input("How much have Snazzy Autos offered for your car (Rs)? ")
# if input isn't a number, display error
if (not trade_amount.isnumeric()):
print("Invalid input. Please enter a number.")
trade_amount = None
# if input is a number outside of the acceptable range, display an error
elif (int(trade_amount) <= 0):
print("Invalid input. Please enter a number above 0.")
trade_amount = None
trade_amount = float(trade_amount)
else:
car_trade = False
trade_amount = 0
# display total cost without discounts
print("Cost: Rs %.2f" % cost)
# calculate discount: 10% if existing customer and 5% if car isn't being traded in
discount = (cost * 0.1 if existing_customer else 0) + (cost * 0.05 if not car_trade else 0)
# display total discount
print("Discount: Rs %.2f" % discount)
# if car is traded in, print trade-in amount
if car_trade: print("Trade-in amount: Rs %.2f" % trade_amount)
# print total cost
print("\nTotal cost: Rs %.2f" % (cost - discount - trade_amount))
# ask which payment option user wants
print("Which payment option would you like?")
cashback = cost * 0.01
if (cashback > optional_extras_cost):
print("(1) Pay full amount now - 1%s cashback - total cost Rs %.2f - one payment - including cashback of Rs %.2f | or free optional extras - total cost %.2f - one payment - including discount of %.2f" % ("%", cost * 0.99 - discount, cashback, cost - optional_extras_cost - discount, optional_extras_cost))
else:
print("(1) Pay full amount now - free optional extras - total cost %.2f - one payment - including discount of %.2f | or 1%s cashback - total cost Rs %.2f - one payment - including cashback of Rs %.2f" % (cost - optional_extras_cost - discount, optional_extras_cost, "%", cost * 0.99 - discount, cashback))
print("(2) Monthly payements for four years - no charge - total cost Rs %.2f - payments of Rs %.2f - 48 payments" % (cost - discount, cost / 48))
print("(3) Monthly payments for seven years - 5%s charge - total cost Rs %.2f - payments of Rs %.2f - 80 payments" % ("%", cost * 1.05 - discount, cost * 1.05 / 84))
# keep asking until valid input entered
payment_option = None
while (not payment_option):
# ask for input
payment_option = input()
# if input isn't a number, display error
if (not payment_option.isnumeric()):
print("Invalid input. Please enter a number.")
payment_option = None
# if input is a number outside acceptable range, display error
elif (not 0 < int(payment_option) < 4):
print("Invalid input. Please enter a number between 1 and 3.")
payment_option = None
# calculate total cost, adjusted for chosen payment option
payment_option = int(payment_option)
if (payment_option == 3):
cost *= 1.05
# ask user if they want 1% cashback or free optional extras if they pay upfront
if (payment_option == 1):
print("Which option would you like?")
if (cashback > optional_extras_cost):
print("(1) Cashback - save Rs %.2f - total cost Rs %.2f" % (cashback, cost * 0.99 - discount))
print("(2) Free optional extras - save Rs %.2f - total cost Rs %.2f" % (optional_extras_cost, cost - optional_extras_cost - discount))
else:
print("(1) Free optional extras - save Rs %.2f - total cost Rs %.2f" % (optional_extras_cost, cost - optional_extras_cost - discount))
print("(2) Cashback - save Rs %.2f - total cost Rs %.2f" % (cashback, cost * 0.99 - discount))
# keep asking until valid input entered
cashback_or_free_extras = None
while (not cashback_or_free_extras):
cashback_or_free_extras = input()
# if input isn't a number, display error
if (not cashback_or_free_extras.isnumeric()):
print("Invalid input. Please enter a number.")
cashback_or_free_extras = None
# if input is number outside of acceptable range, display error
elif (not 0 < int(cashback_or_free_extras) < 3):
print("Invalid input. Please enter either 1 or 2.")
cashback_or_free_extras = None
# calculate total cost and store order discount message
cashback_or_free_extras = int(cashback_or_free_extras)
if ((cashback > optional_extras_cost and cashback_or_free_extras == 1) or (cashback <= optional_extras_cost and cashback_or_free_extras == 2)):
cost = cost * 0.99
order_message = "1%s cashback of Rs %.2f" % ("%", cashback)
else:
cost = cost - optional_extras_cost
order_message = "free optional extras (Rs %.2f)" % optional_extras_cost
# store order discount message
if payment_option == 2:
order_message = "no discount"
elif payment_option == 3:
order_message = "5%s price increase of Rs %.2f" % ("%", cost / 1.05 * 0.05)
cost = cost - discount - trade_amount
# print order summary
print("\nOrder summary:")
print("Total cost: Rs %.2f - with %s" % (cost, order_message))
payments_number = [1, 48, 80]
print("%d payment(s) of Rs %.2f" % (payments_number[payment_option - 1], cost / payments_number[payment_option - 1]))
# 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.")
# reset input to blank, so user is asked again
tmp = None
# return input
return tmp
# 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]))
# 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 run(self):
# 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)
devices_dict = {**phones_dict , **tablets_dict}
# dictionary for SIMs
sims_dict = {"SMNO": (0.00, "SIM Free (no SIM card purchased)"),
"SMPG": (9.99, "Pay As You Go (SIM card purchased)")}
# dictionary for cases
case_dict = {"CSST": (0.00, "Standard"),
"CSLX": (50.00, "Luxury")}
# dictionary for chargers
charger_dict = {"CGCR": (19.99, "Car"),
"CGHM": (15.99, "Home")}
# total cost
total = 0
# total amount of money saved
saving = 0
# ensure user is asked for input until they don't want more devices
run = True
# total number of devices ordered
devices = 0
# run until user doesn't want any more devices
while run:
# get chosen device
print("Choose an option:")
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
# 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()