0% found this document useful (0 votes)
1 views5 pages

Programming Assignment

The document describes two Python functions: one for calculating the circumference of a circle based on a given radius, and another for calculating the total price of items purchased with applicable discounts. The circumference function uses the formula C = 2 * π * r, while the total price function applies discounts based on the number of different items purchased. Both functions demonstrate key programming concepts such as function parameters, conditional statements, and arithmetic operations.

Uploaded by

ndirangu.rukuru
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views5 pages

Programming Assignment

The document describes two Python functions: one for calculating the circumference of a circle based on a given radius, and another for calculating the total price of items purchased with applicable discounts. The circumference function uses the formula C = 2 * π * r, while the total price function applies discounts based on the number of different items purchased. Both functions demonstrate key programming concepts such as function parameters, conditional statements, and arithmetic operations.

Uploaded by

ndirangu.rukuru
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Part 1: Circumference Function

Function to calculate and print circumference

Formula used: C = 2 * π * r, where π = 3.14159

def print_circum(radius):

pi = 3.14159 # constant value of pi

circumference = 2 * pi * radius # calculation

print("Radius:", radius, "-> Circumference:", circumference)

Function calls with different radius values

print_circum(5)

print_circum(10)

print_circum(2.5)

Output

Radius: 5 -> Circumference: 31.4159

Radius: 10 -> Circumference: 62.8318

Radius: 2.5 -> Circumference: 15.70795

Technical Explanation

The function print_circum(radius) takes a single parameter called radius. Inside the function,

a constant value of π (3.14159) is stored in the variable pi. The circumference is computed

using the formula 2 * pi * radius. The result is stored in the variable circumference and

printed alongside the input radius.


When the function is called three times with different arguments (5, 10, and 2.5), Python

substitutes each value into the parameter radius, evaluates the expression, and prints the

corresponding result. This demonstrates how functions reuse logic efficiently with different

inputs. The output confirms that the formula is applied correctly for each radius.

Part 2: Online Store Catalog Function

Function to calculate total price based on purchase type

def calculate_total(item1=0, item2=0, item3=0):

Prices of items

price1 = 100

price2 = 200

price3 = 300

Calculate subtotal

subtotal = (item1 * price1) + (item2 * price2) + (item3 * price3)

Count how many different items are purchased

item_count = sum([1 for item in [item1, item2, item3] if item > 0])

Apply discounts

if item_count == 3:

discount = 0.25 # 25% discount for gift pack

elif item_count == 2:
discount = 0.10 # 10% discount for combo

else:

discount = 0.0 # no discount

discount_amount = subtotal * discount

total = subtotal - discount_amount

Print output

print("Subtotal:", subtotal)

print("Discount Applied:", discount * 100, "%")

print("Final Total:", total)

Example calls

print("--- Single Item Purchase ---")

calculate_total(item1=1)

print("\n--- Combo Purchase (2 items) ---")

calculate_total(item1=1, item2=1)

print("\n--- Gift Pack (3 items) ---")

calculate_total(item1=1, item2=1, item3=1)

Output

--- Single Item Purchase ---

Subtotal: 100
Discount Applied: 0.0 %

Final Total: 100.0

--- Combo Purchase (2 items) ---

Subtotal: 300

Discount Applied: 10.0 %

Final Total: 270.0

--- Gift Pack (3 items) ---

Subtotal: 600

Discount Applied: 25.0 %

Final Total: 450.0

Explanation

This function, calculate_total, demonstrates the use of variables, conditional statements,

arithmetic operators, and function parameters in Python. The function takes three optional

parameters (item1, item2, and item3), each representing the quantity of a specific product.

Default values are set to zero, meaning no item is selected unless specified.

Inside the function, fixed prices are assigned to each item. The subtotal is calculated using

multiplication and addition operators, combining the quantities and prices of selected items.

A list comprehension is used to count how many distinct items were purchased by checking

which quantities are greater than zero.


Based on the number of unique items, conditional statements (if, elif, else) determine the

appropriate discount. If all three items are purchased, a 25% discount is applied. If two

unique items are purchased, a 10% discount is applied. Otherwise, no discount is given.

The discount amount is calculated by multiplying the subtotal by the discount rate, and the

final total is obtained by subtracting this amount from the subtotal. The function prints a clear

breakdown of the subtotal, discount percentage, and final total.

This function illustrates key programming concepts such as decision-making, modular

design, and code reuse. It also shows how business logic (discount rules) can be implemented

using conditional structures, making it highly relevant for real-world applications like e-

commerce systems.

References

Downey, A. B. (2015). Think Python: How to think like a computer scientist (2nd ed.). Green

Tea Press. [Link]

Python Software Foundation. (2024). Python documentation. [Link]

You might also like