CS 1101 - Programming Assignment Unit 2
Course:
Programming Fundamentals (CS 1101)
NAME: PRAISEGOD SHUMBA
University: University of the People
Part 1: Calculating and Printing the Circumference of a Circle
The task requires writing a Python function named `print_circum` that:
- Accepts a single argument (the circle's radius).
- Calculates the circumference using the formula $$ C = 2\pi r $$, where $$ \pi = 3.14159 $$ (rounded to five
decimal places).
- The function must be called three times with different radius values.
Below is the code,
### **Code for `print_circum` Function**
```python
def print_circum(radius):
pi = 3.14159 # Define the value of π rounded to five decimal places
circumference = 2 * pi * radius # Calculate circumference using the formula
print(f"The circumference of a circle with radius {radius} is: {circumference}")
```
### **Function Calls and Output**
```python
print_circum(5)
print_circum(2)
print_circum(52)
```
**Sample Output:**
```
The circumference of a circle with radius 5 is: 31.4159
The circumference of a circle with radius 2 is: 12.56636
The circumference of a circle with radius 52 is: 326.72535999999997
```
---
### **Technical Explanation**
- **Function Definition:**
The function `print_circum` is defined to take one parameter, `radius`, which represents the radius of the circle.
- **Value of π:**
The value of π is set to 3.14159, as required by the assignment.
- **Circumference Calculation:**
The circumference is calculated using the formula $$ C = 2\pi r $$. This is achieved by multiplying 2, π, and the
radius.
- **Output:**
The function prints the result in a formatted string, clearly showing both the radius used and the calculated
circumference.
- **Three Function Calls:**
The function is called three times with different values for the radius (5, 2, and 52). The function for each code is
explained.
Part 2.
### Function Explanation
1. **Initialization**: The function initializes the prices for three items (`item1`, `item2`, `item3`) and calculates the
total price for all possible two-item combinations (`combo1`, `combo2`, `combo3`) and a gift pack containing all
three items (`combo4`).
2. **Discounts**: It defines discount rates for combo packs (10%) and gift packs (25%).
3. **Discounted Prices**: It calculates the final prices for each combo and the gift pack after applying the
respective discounts.
4. **Output**: It prints a formatted catalog to the console, including individual item prices, combo prices (after the
10% discount), and the gift pack price (after the 25% discount).
5. **Store Details**: Includes the store name and contact information for delivery.
### Technical Explanation
* The function uses basic arithmetic operations to calculate prices and discounts.
* String concatenation is used to create the catalog output, combining product names and calculated prices.
* The `str()` function is used to convert
numeric values to strings for printing.
* The `\t` character is used for horizontal tabulation to align the text in the catalog.
* The function illustrates how to apply discounts to different product combinations and present them in a user-
friendly format.
Reference
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea
https://greenteapress.com/thinkpython2/thinkpython2.pdf