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

Programming Assignment Unit 2

Uploaded by

Tsega Teklewold
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)
3 views

Programming Assignment Unit 2

Uploaded by

Tsega Teklewold
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/ 4

Part 1

# Defining the function print_circum to calculate the circumference

def print_circum(radius):

# using the formula for circumference: 2πr

pi = 3.14159 # value of pi rounded to five decimal places

circumference = 2 * pi * radius # calculate the circumference

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

# Call the function with different values for radius

print_circum(5) # Calling with radius = 5

print_circum(10) # Calling with radius = 10

print_circum(15) # Calling with radius = 15

Explanation:

1. Function Definition:

o The print_circum function takes one argument, radius, which is the radius of

the circle.

o Inside the function, we use the formula for the circumference of a circle, C=2πr

where π is approximated to 3.14159.

o The result is printed using Python's formatted string feature, rounding the

circumference to two decimal places using :.2f formatting.

2. Function Calls:
o The function is called three times, each with a different radius (5, 10, and 15).

Each call calculates the circumference for a circle with the given radius.

3. Output:

o The function prints the circumference for each radius passed to it. The output is

formatted to show two decimal places for clarity.

Part 2
def print_catalog():

"""

This function displays the catalog for Tsega's Online Store with the prices for individual items

and the discounted prices for combo packs and the gift pack.

"""

# Prices for individual items

item_prices = {

"Item 1": 200,

"Item 2": 250,

"Item 3": 300

# Combo prices (before discount)


combo_prices = {

"Combo 1 (Item 1 + Item 2)": item_prices["Item 1"] + item_prices["Item 2"],

"Combo 2 (Item 1 + Item 3)": item_prices["Item 1"] + item_prices["Item 3"],

"Combo 3 (Item 2 + Item 3)": item_prices["Item 2"] + item_prices["Item 3"]

# Gift Pack price (before discount)

gift_pack_price = item_prices["Item 1"] + item_prices["Item 2"] + item_prices["Item 3"]

# Discount calculations

combo_discount = 0.10 # 10% discount for combo packs

gift_pack_discount = 0.25 # 25% discount for gift pack

# Calculate discounted prices for combos

combo_prices_discounted = {combo: price * (1 - combo_discount) for combo, price in

combo_prices.items()}

gift_pack_price_discounted = gift_pack_price * (1 - gift_pack_discount)

# Print the catalog header

print("Tsega's Online Store")

print("=" * 50)

print("Contents".ljust(40) + "Price (USD)")

print("-" * 50)
# Print the prices of individual items

for item, price in item_prices.items():

print(f"{item.ljust(40)}${price}")

# Print the discounted prices for combo packs

for combo, discounted_price in combo_prices_discounted.items():

print(f"{combo.ljust(40)}${discounted_price:.2f}")

# Print the discounted price for the gift pack

print(f"Gift Pack (Item 1 + Item 2 + Item 3)".ljust(40) + f"$

{gift_pack_price_discounted:.2f}")

# Call the function to display the catalog

print_catalog()

References

 Downey, A. (2015). Think Python: How to Think Like a Computer Scientist. Green Tea
Press. Link to book.
 Python Documentation. https://docs.python.org.

You might also like