Back to 课程

Computer Science GCES EDEXCEL

0% Complete
0/0 Steps
  1. Decomposition And Abstraction Edexcel
    2 主题
  2. Algorithms Edexcel
    11 主题
  3. Truth Tables Edexcel
    3 主题
  4. Binary Edexcel
    6 主题
  5. Data Representation Edexcel
    4 主题
  6. Data Storage And Compression Edexcel
    2 主题
  7. Hardware Edexcel
    5 主题
  8. Software Edexcel
    3 主题
  9. Programming Languages Edexcel
    2 主题
  10. Networks Edexcel
    7 主题
  11. Network Security Edexcel
    2 主题
  12. Environmental Issues Edexcel
    1 主题
  13. Ethical And Legal Issues Edexcel
    3 主题
  14. Cybersecurity Edexcel
    2 主题
  15. Develop Code Edexcel
    6 主题
  16. Constructs Edexcel
    4 主题
  17. Data Types And Data Structures Edexcel
    5 主题
  18. Operators Edexcel
    1 主题
  19. Subprograms Edexcel
    2 主题
课 Progress
0% Complete

Exam code:1CP2

Using Decomposition & Abstraction to Analyse,
Understand & Solve Problems

Examiner Tips and Tricks

Before attempting to use decomposition and abstraction it is important to understand the process by reading here

Task

  • Write a program that calculates the total cost of a pizza order. The program should include the following features:

    • Size: The user can choose from three sizes (small, medium, and large) with different base prices

    • Toppings: The user can choose any number of toppings from a list, with each topping having a set price

    • Discount: There is a 10% discount applied to orders over £20

Abstraction

  • Create a function that calculates and returns the base price of the pizza by taking the size as an input

Decomposition

  • Break down the task in to smaller, more manageable pieces

  • For example, using separate functions to:

    • Get the user input for pizza size and toppings

    • Calculate cost of toppings

    • Apply discount (if applicable)

    • Output final order total

Python code

def calculate_pizza_price(size):

# ———————————————————————–
# This function takes the pizza size as input and returns the base price

# ———————————————————————–

base_prices = {"small": 5, "medium": 10, "large": 20}

return base_prices[size]

def get_user_choice(options):

# ———————————————————————–

# This function displays options and gets valid user input from the provided list

# ———————————————————————–

while True:

choice = input("Choose from "+", ".join(options)+": ").lower()

if choice in options:

return choice

else:

print("Invalid choice. Please try again.")

def calculate_topping_cost(toppings, topping_prices):

# ———————————————————————–

# This function calculates the total cost of selected toppings

# ———————————————————————–

total_cost = 0

for topping in toppings:

total_cost = total_cost + topping_prices[topping]

return total_cost

def apply_discount(total_cost):

# ———————————————————————–

# This function applies a 10% discount if the total cost exceeds £20

# ———————————————————————–

if total_cost > 20:

discount = total_cost * 0.1

total_cost = total_cost - discount

return total_cost

def main():

# ———————————————————————–

# This function coordinates the program flow and calls other functions

# ———————————————————————–

pizza_size = get_user_choice(["small", "medium", "large"])

base_price = calculate_pizza_price(pizza_size)

toppings_available = ["pepperoni", "mushrooms", "onions"]

toppings = []

while True:

add_topping = input("Add a topping (y/n)?").lower()

if add_topping == 'n':

break

else:

topping = get_user_choice(toppings_available)

toppings.append(topping)

topping_cost = calculate_topping_cost(toppings, {"pepperoni": 1, "mushrooms": 0.5, "onions": 0.75})

total_cost = base_price + topping_cost

total_cost = apply_discount(total_cost)

print("Your total pizza cost is: £",total_cost)

# ———————————————————————–

# Main program starts here

# ———————————————————————–

main()

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注