0% found this document useful (0 votes)
10 views9 pages

Prog Unit2

The document outlines two programming assignments in Python: the first involves creating a function to calculate and print the circumference of a circle based on a given radius, while the second focuses on generating a catalog of products with pricing and discounts. Each function includes detailed explanations of the code, expected outputs, and the logic behind the calculations. Additionally, references to relevant programming literature are provided.

Uploaded by

katibabrian207
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)
10 views9 pages

Prog Unit2

The document outlines two programming assignments in Python: the first involves creating a function to calculate and print the circumference of a circle based on a given radius, while the second focuses on generating a catalog of products with pricing and discounts. Each function includes detailed explanations of the code, expected outputs, and the logic behind the calculations. Additionally, references to relevant programming literature are provided.

Uploaded by

katibabrian207
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/ 9

Programming Assignment Unit 2

Vandelun Amokaha (Instructor)

University of The People

CS 1101 – Programming Fundamentals

Kevin Kipkirui

PART 1
Here’s how to create the function print_circum in Python, along with an explanation of

the code, its execution, and the resulting outputs:

Code for the print_circum Function

# Function definition

def print_circum(radius):

# Define π to 5 decimal places

pi = 3.14159

# Calculate the circumference

circumference = 2 * pi * radius

# Print the calculated circumference

print(f"The circumference of a circle with radius {radius}

is {circumference:.5f}")

# Call the function three times with different radii

print_circum(5) # Radius = 5 units

print_circum(10) # Radius = 10 units

print_circum(15) # Radius = 15 units

Explanation of the Code

1. Function Definition:

o print_circum takes one parameter, radius, which represents

the radius of a circle.


o Inside the function, π (pi) is defined as 3.14159, rounded to 5

decimal places.

2. Circumference Calculation:

o The formula for circumference, 2 * π * radius, is

implemented using the radius argument.

3. Output Formatting:

o The result is printed using Python's formatted string literals (f-

strings). The .5f ensures the result is rounded to 5 decimal

places.

4. Function Calls:

o The function is called three times with radii of 5, 10, and 15 to

demonstrate its operation on different inputs.

Expected Output

When the function is run, it will output:

The circumference of a circle with radius 5 is 31.41590

The circumference of a circle with radius 10 is 62.83180

The circumference of a circle with radius 15 is 94.24770

Technical Explanation

 Input: The function accepts a single numeric argument (radius).


 Process:

o The circumference is computed using the constant value of π (3.14159) and

the input radius.

o This result is formatted to 5 decimal places.

 Output: Each call to the function prints the calculated circumference directly to the

console.

PART 2

Here is a custom Python function for the provided scenario, with an explanation and the

expected output:
Code:

def generate_catalog():

# Define the prices of individual items

item1_price = 200.0

item2_price = 400.0

item3_price = 600.0

# Calculate combo prices with discounts

combo1_price = (item1_price + item2_price) * 0.9 # 10%

discount for Item 1 + 2

combo2_price = (item2_price + item3_price) * 0.9 # 10%

discount for Item 2 + 3

combo3_price = (item1_price + item3_price) * 0.9 # 10%

discount for Item 1 + 3

gift_pack_price = (item1_price + item2_price +

item3_price) * 0.75 # 25% discount for all 3 items

# Print the catalog

print("Online Store")

print("-------------------")

print("Product(S) Price")

print(f"Item 1 {item1_price}")

print(f"Item 2 {item2_price}")

print(f"Item 3 {item3_price}")

print(f"Combo 1(Item 1 + 2) {combo1_price:.1f}")


print(f"Combo 2(Item 2 + 3) {combo2_price:.1f}")

print(f"Combo 3(Item 1 + 3) {combo3_price:.1f}")

print(f"Combo 4(Item 1 + 2 + 3) {gift_pack_price:.1f}")

print("-------------------")

print("For delivery Contact: 98764678899")

# Call the function

generate_catalog()

Output:

When executed, the code generates the following output:

Online Store

-------------------

Product(S) Price

Item 1 200.0

Item 2 400.0

Item 3 600.0

Combo 1(Item 1 + 2) 540.0

Combo 2(Item 2 + 3) 900.0

Combo 3(Item 1 + 3) 720.0

Combo 4(Item 1 + 2 + 3) 900.0

-------------------

For delivery Contact: 98764678899


Explanation:

1. Input Values:

o The base prices for Item 1, Item 2, and Item 3 are stored in variables.

2. Discount Calculations:

o For combos with two items, a 10% discount is applied (price * 0.9).

o For the gift pack with all three items, a 25% discount is applied (price * 0.75).

3. Dynamic Formatting:

o Formatted output ensures proper alignment and displays prices with a single

decimal place for clarity.

4. Features Illustrated:

o Modular design using a single function to define and display catalog details.

o Mathematical calculations directly applied in code to derive discounted prices.

o User-friendly formatting that mimics real-world catalogs.


References

1. Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree

Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf

2. Zelle, J. M. (2017). Python programming: An introduction to computer science (3rd

ed.). Franklin, Beedle & Associates.

3. Sweigart, A. (2020). Automate the boring stuff with Python: Practical programming

for total beginners (2nd ed.). No Starch Press. https://automatetheboringstuff.com/

You might also like