0% found this document useful (0 votes)
8 views

Assignment 2

The document describes two Python functions: 1) A print_circum function that calculates and prints the circumference of a circle given its radius. It is tested with radii of 5, 7.5, and 10 units. 2) A calculate_total_cost function that calculates the total cost of purchases from a catalog based on quantities of three items and applies different discounts - 10% for two items, 25% for all three items. It is tested with an example purchase.

Uploaded by

xpayne4
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)
8 views

Assignment 2

The document describes two Python functions: 1) A print_circum function that calculates and prints the circumference of a circle given its radius. It is tested with radii of 5, 7.5, and 10 units. 2) A calculate_total_cost function that calculates the total cost of purchases from a catalog based on quantities of three items and applies different discounts - 10% for two items, 25% for all three items. It is tested with an example purchase.

Uploaded by

xpayne4
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
You are on page 1/ 3

Part 1: Circumference Calculator

I've made a function called print_circum that figures out and shows the circumference of a circle
when you tell it the circle's radius. I tried it with different radii:

 For a radius of 5 units, it said the circumference is about 31.41590 units.


 With a radius of 7.5 units, it found a circumference of about 47.12385 units.
 And for a radius of 10 units, it gave a circumference of about 62.83180 units.

def print_circum(radius):

circumference = 2 * 3.14159 * radius

print(f"The circumference of a circle with radius {radius} is {circumference:.5f}")

# Calling the function with different values for radius

print_circum(5)

print_circum(7.5)

print_circum(10)

# Output for each call:

# The circumference of a circle with radius 5 is 31.41590

# The circumference of a circle with radius 7.5 is 47.12385

# The circumference of a circle with radius 10 is 62.83180

Part 2: Catalog and Discount Calculator


I created a function, calculate_total_cost, for your company's catalog. It calculates the total cost
based on how many of three items you want to buy: item1, item2, and item3. Here's how the
discounts work:

 No discount if you buy items individually.


 10% discount if you buy a combo pack with two unique items.
 25% discount if you buy the gift pack with all three items.

def calculate_total_cost(item1, item2, item3):

item_prices = {

"Item1": 10.00,

"Item2": 15.00,

"Item3": 20.00

total_cost = 0

# Calculate the cost of individual items

total_cost += item_prices["Item1"] * item1

total_cost += item_prices["Item2"] * item2

total_cost += item_prices["Item3"] * item3

# Calculate discount for combo packs

if item1 == 1 and item2 == 1:

total_cost *= 0.9 # 10% discount for a combo of two unique items

elif item1 == 1 and item3 == 1:

total_cost *= 0.9

elif item2 == 1 and item3 == 1:

total_cost *= 0.9

# Calculate discount for gift pack

if item1 == 1 and item2 == 1 and item3 == 1:


total_cost *= 0.75 # 25% discount for a gift pack

return total_cost

# Example usage:

item1_quantity = 2

item2_quantity = 1

item3_quantity = 0

total_cost = calculate_total_cost(item1_quantity, item2_quantity, item3_quantity)

print(f"Total cost: ${total_cost:.2f}")

This code defines a calculate_total_cost function that calculates the total cost of items based on
the given quantities and applies discounts as specified in your scenario. You can adjust
item1_quantity, item2_quantity, and item3_quantity to simulate different purchases.

Reference: Python Crash Course by Eric Matthes

You might also like