Part 2
Here is the function to generate the catalogue. All explanations have been included in the form of code comments
within the code
Part 1
>>> #For a function to print the circumference given the radius as an argument the function first needs to have a
parameter representing the radius. Then, it is required that using the radius parameter, the function calculates the
circumference using the formula "2*pi*radius". To improve our function that it only gives the circumference
rounded to five decimal places we need to include the round() function. Then we need to return this value for it to
be printed as shown below.
Code
>>> def print_circum(radius):
... return round( 2 * 3.14159 * radius, 5)
>>> # The round function takes the following format: "round(value_to_round, decimal_places)," as seen in the
above equation, where the value is the result of performing the circumference operation and the number of decimal
places is five.
Output
Code
def confectionary_catalogue():
#Declaring product names and prices as variables
item_1 = "Candy"
item_2 = "Lollipop"
item_3 = "Chocolate"
item_1_price = 200.0
item_2_price = 400.0
item_3_price = 600.0
#Declaring of combo names by combining individual product names which were assigned to variables earlier on
combo_1 = f"({item_1} + {item_2})"
combo_2 = f"({item_2} + {item_3})"
combo_3 = f"({item_1} + {item_3})"
combo_4 = f"({item_1} + {item_2} + {item_3})"
#Calculating of prices of different combo prices making sure that two products cost 90 percent of the original
price by multiplying the sum by 0.9 and rounding the value to one decimal place using the round function.
Ensuring that 3 products have a 25% discount by multiplying the sum off all prices with 0.75 and rounding of the
result to one decimal place
combo_1_price = round((item_1_price + item_2_price) * 0.9, 1)
combo_2_price = round((item_2_price + item_3_price) * 0.9, 1)
combo_3_price = round((item_1_price + item_3_price) * 0.9, 1)
combo_4_price = round((item_1_price + item_2_price + item_3_price) * 0.75, 1)
#Function to ensure proper spacing between product prices and names
def product_price_spacing(product_string):
#Declaring of Ideal space between product names and prices which was gotten by selecting a number that
would clearly space between the titles Product(s) and Price.
spacing = 37
#Identifying length of product name
string_length = len(product_string)
if string_length > 10 :
# Making sure that for strings longer than 10 which is the length of the title Product(s) (The ones for the
combos) have the length of the word combo and the space after it considered in determining the spacing.
string_length += 8
#Additional length is the length exceeding the length of the Product(s) title
additional_length = string_length - 10
#New spacing is the spacing required after examining the number of characters exceeding the length of the
Product(s) title
new_spacing = spacing - additional_length
#Multiplying the spaces with the value of spaces required
return " " * new_spacing
elif string_length < 10 :
#Identifying the value by which the string is shorter than the Product(s) title and multiplying it with the
spaces to give ideal spacing
return " " * (spacing + (10 - string_length))
elif string_length == 10:
#Since the length is equal to that of the Product(s) title it should be multiplied by the number of spaces as
determined at the beginning of the function
return " " * spacing
else :
#Return otherwise to prevent exceptional errors
return
#Printing of the catalogue with spaces calculated by the product_price_spacing() function and the \n special
character to move to new lines
return ("Online Store\n"
"----------------------\n"
"Product(S) Price\n"
f"{item_1}{product_price_spacing(item_1)}{item_1_price}\n"
f"{item_2}{product_price_spacing(item_2)}{item_2_price}\n"
f"{item_3}{product_price_spacing(item_3)}{item_3_price}\n"
f"Combo 1 {combo_1}{product_price_spacing(combo_1)}{combo_1_price}\n"
f"Combo 2 {combo_2}{product_price_spacing(combo_2)}{combo_2_price}\n"
f"Combo 3 {combo_3}{product_price_spacing(combo_3)}{combo_3_price}\n"
References
Information on how to use the round function was derived from Alan Ouko's article “How to Round to 2 Decimal
Places in Python,” published on August 8, 2024.
Information on Python functions was derived from “Think Python” 2nd Edition by Allen Downey.
f"Combo 4 {combo_4}{product_price_spacing(combo_4)}{combo_4_price}\n"
"________________________\n"
"For delivery Contact:98764678899")
print(confectionary_catalogue())
Output