Programming Assignment Unit 2
PART 1
# Function to calculate and print the circumference of a circle
def print_circum(radius):
"""
This function calculates the circumference of a circle given its radius.
Formula: Circumference = 2 * π * radius
"""
# Define π rounded to five decimal places
pi = 3.14159
# Calculate the circumference using the formula
circumference = 2 * pi * radius
# Print the calculated circumference formatted to two decimal places
print(f"The circumference of a circle with radius {radius} is: {circumference:.2f}")
# Call the function with three different radius values
print_circum(5) # Test case 1: Radius = 5
print_circum(10) # Test case 2: Radius = 10
print_circum(20) # Test case 3: Radius = 20
Explanation of the Code
1. Function Definition: The function print_circum is defined to take one
parameter, radius, which represents the radius of the circle.
2. Docstring: The docstring explains what the function does and provides the formula used
for calculating the circumference.
3. π Definition: The value of π is defined as 3.14159, which is rounded to five decimal
places.
4. Circumference Calculation: The circumference of the circle is calculated using the
formula ( C = 2 \pi r ), where r is the radius passed to the function.
5. Output: The result is printed in a formatted string that shows the radius and the
calculated circumference, rounded to two decimal places for clarity.
6. Function Calls: The function is called three times with different radius values (5, 10, and
20) to demonstrate its functionality.
Output:
PART 2
def calculate_total_price(item_prices, items_purchased):
"""
This function calculates the total price for items purchased based on given pricing and
discount rules.
:param item_prices: A dictionary containing the prices of individual items.
:param items_purchased: A list containing the items purchased. It can include:
- Individual items (e.g., ['item1'])
- A combo of two unique items (e.g., ['item1', 'item2'])
- A gift pack (e.g., ['item1', 'item2', 'item3'])
:return: The total price after applying discounts.
"""
# Calculate the total price without discounts
total_price = sum(item_prices[item] for item in items_purchased)
# Apply discounts based on the number of items purchased
if len(items_purchased) == 2: # Combo of two unique items
discount = 0.10 # 10% discount
total_price *= (1 - discount)
elif len(items_purchased) == 3: # Gift pack
discount = 0.25 # 25% discount
total_price *= (1 - discount)
return total_price
# Prices of individual items
item_prices = {
'item1': 20.00, # Price of item 1
'item2': 30.00, # Price of item 2
'item3': 50.00 # Price of item 3
}
# Example purchases
individual_purchase = ['item1'] # No discount
combo_purchase = ['item1', 'item2'] # 10% discount
gift_pack_purchase = ['item1', 'item2', 'item3'] # 25% discount
# Calculate totals
total_individual = calculate_total_price(item_prices, individual_purchase)
total_combo = calculate_total_price(item_prices, combo_purchase)
total_gift_pack = calculate_total_price(item_prices, gift_pack_purchase)
# Output the results
print(f"Total price for individual purchase: ${total_individual:.2f}")
print(f"Total price for combo purchase: ${total_combo:.2f}")
print(f"Total price for gift pack purchase: ${total_gift_pack:.2f}")
Output:
Explanation of Features Illustrated by the Function
1. Function Definition: The function calculate_total_price is defined to take two
parameters: a dictionary of item prices and a list of items purchased.
2. Price Calculation: The total price is calculated by summing the prices of the items in
the items_purchased list.
3. Discount Application:
If the customer purchases two unique items, a 10% discount is applied.
If the customer purchases a gift pack (all three items), a 25% discount is applied.
4. Return Value: The function returns the total price after applying any applicable
discounts.
5. Example Purchases: The code demonstrates three different types of purchases:
An individual purchase with no discount.
A combo purchase with two items, applying a 10% discount.
A gift pack purchase with all three items, applying a 25% discount.
6. Formatted Output: The results are printed in a formatted manner, showing the total
prices for each type of purchase.
SOURCES:
1. Software Development Practices
"Clean Code: A Handbook of Agile Software Craftsmanship" by Robert C. Martin
2. E-commerce Pricing Strategies
"Pricing Strategies: A Marketing Approach" by Robert M. Schindler
3. Python Programming Best Practices
"Python Crash Course" by Eric Matthes
4. User Experience in E-commerce
"Don't Make Me Think: A Common Sense Approach to Web Usability" by Steve Krug
5. Discounting Strategies and Consumer Behavior
"The Psychology of Pricing: A Guide to Pricing Strategies" by Simon-Kucher & Partners