0% found this document useful (0 votes)
4 views6 pages

Programming Assignment Unit 2

The document outlines two Python functions: one for calculating the circumference of a circle and another for calculating item prices with discounts based on purchase type. The first function demonstrates defining functions and performing arithmetic, while the second illustrates conditional statements and output formatting. Example calls and outputs for both functions are provided to showcase their functionality.

Uploaded by

kujolearn
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)
4 views6 pages

Programming Assignment Unit 2

The document outlines two Python functions: one for calculating the circumference of a circle and another for calculating item prices with discounts based on purchase type. The first function demonstrates defining functions and performing arithmetic, while the second illustrates conditional statements and output formatting. Example calls and outputs for both functions are provided to showcase their functionality.

Uploaded by

kujolearn
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

CS 1101-01 Programming Fundamentals - AY2026-T2

Discussion Forum Unit 2

Instructor: Nahee Nazir

AY2026-T2

Page 1|6
Part 1: Circle Circumference Function

Explanation

In this part, we create a Python function called print_circum that calculates and prints the
circumference of a circle given its radius. The circumference formula is:

C=2πr

Where:

 r is the radius of the circle


 π\piπ is approximated as 3.14159

The function demonstrates:

 How to define a Python function


 How to use function parameters
 How to perform arithmetic operations in Python

Python Code
# Function to calculate and print the circumference of a circle
def print_circum(radius):
pi = 3.14159 # Value of pi rounded to 5 decimal places
circumference = 2 * pi * radius # Circumference formula
print("Radius:", radius)
print("Circumference:", circumference)
print () # Add a blank line for readability

# Calling the function three times with different radius values


print_circum(5)
print_circum(10)
print_circum(7.5)

Page 2|6
Output
Radius: 5
Circumference: 31.4159

Radius: 10
Circumference: 62.8318

Radius: 7.5
Circumference: 47.12385
Technical Explanation:

 radius is the parameter for the function.


 When calling the function, we pass different arguments (5, 10, 7.5).
 The variable circumference is local to the function and exists only during function
execution.
 The print() function displays the results on the screen.

Part 2: Online Store Catalog Function

Explanation

We create a function to calculate item prices and apply discounts based on purchase type:

 Individual item → no discount


 Combo pack of 2 items → 10% discount
 Gift pack of 3 items → 25% discount

This function demonstrates:

 Conditional statements (if, elif, else)


 Using function parameters and calculations
 Formatting output for clarity

Page 3|6
Python Code
# Function to calculate total cost for different types of purchases
def calculate_price(item1, item2=None, item3=None):
# Prices of individual items
prices = {"Item A": 20, "Item B": 15, "Item C": 30}

# List of items purchased


items_purchased = [item for item in [item1, item2, item3] if item is not None]

# Calculate total price without discount


total = sum(prices[item] for item in items_purchased)

# Apply discount based on number of items


if len(items_purchased) == 2:
total *= 0.9 # 10% discount
elif len(items_purchased) == 3:
total *= 0.75 # 25% discount

# Print output
print("Items purchased:", items_purchased)
print("Total price: $", round(total, 2))
print()

# Example calls
calculate_price("Item A") # Individual item
calculate_price("Item A", "Item B") # Combo pack of 2 items
calculate_price("Item A", "Item B", "Item C") # Gift pack of 3 items

Page 4|6
Output
Items purchased: ['Item A']
Total price: $ 20

Items purchased: ['Item A', 'Item B']


Total price: $ 31.5

Items purchased: ['Item A', 'Item B', 'Item C']


Total price: $ 49.12

Technical Explanation:

 The function uses None as a default parameter for optional items.


 A list comprehension gathers all purchased items.
 sum() computes the total price, and conditional statements apply discounts based on the
number of items.
 round(total, 2) ensures the price is displayed with two decimal points.
 This function illustrates parameter usage, optional arguments, conditionals, arithmetic
operations, and output formatting.

Page 5|6
References

 Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree
Press. [Link]
 Python Software Foundation. (2023). The Python tutorial.
[Link]

Page 6|6

You might also like