CS1101-Unit02 programming assignment
docx
keyboard_arrow_up
School
University of the People *
*We aren’t endorsed by this school
Course
1101
Subject
English
Date
Jan 9, 2024
Type
docx
Pages
5
Uploaded by BaronEmuPerson965
Part 1:
The code for my print_circum function:
>>> def print_circum(radius):
... print(2 * 3.14159 * radius)
The inputs and outputs to three calls of my print_circum function:
>>> print_circum(1)
6.28318
>>> print_circum(15.5)
97.38929
>>> print_circum(250)
1570.7949999999998
Part 2:
Code of the function I created:
def print_catalog():
item1_price = 200.0
item2_price = 400.0
item3_price = 600.0
combo_discount = 0.10
giftpack_discount = 0.25
combo1_price = item1_price + item2_price - combo_discount * (item1_price + item2_price)
combo2_price = item2_price + item3_price - combo_discount * (item2_price + item3_price)
combo3_price = item1_price + item3_price - combo_discount * (item1_price + item3_price)
combo4_price = item1_price + item2_price + item3_price -
giftpack_discount * (item1_price + item2_price + item3_price)
dash_rule = '- ' * 20
line_rule = '-' * 40
print("Online Store")
print(dash_rule)
print(
f"{'Product(s)':<30}{'Price':>10}",
f"\n{'Item 1':<30}{item1_price:>10}",
f"\n{'Item 2':<30}{item2_price:>10}",
f"\n{'Item 3':<30}{item3_price:>10}",
f"\n{'Combo 1 (item 1 + 2)':<30}{combo1_price:>10}",
f"\n{'Combo 2 (item 2 + 3)':<30}{combo2_price:>10}",
f"\n{'Combo 3 (item 1 + 3)':<30}{combo3_price:>10}",
f"\n{'Combo 4 (item 1 + 2 + 3)':<30}{combo4_price:>10}",
)
print(line_rule)
print("For delivery Contact: 98764678899")
Output of the code:
Online Store
- - - - - - - - - - - - - - - - - - - - Product(s) Price
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
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
A description of what feature(s) the function illustrates:
The function I created is called print_catalog()
. I began with defining a number of
variables in order to set base prices of the individual items, as well as to specify the percentage discount given for combo and gift pack purchases.
Once the base prices were set up, I used them with the arithmetic operators (+, -, *) to define prices for each of the combos that can be purchased. For example, combo1_price = item1_price + item2_price - combo_discount * (item1_price + item2_price) (combo 1 price = price of item 1 + price of item 2, minus combo discount of 10%.)
I used the * repetition operator with strings ‘-‘ and ‘- ‘ to define dash_rule and line_rule variables, which will print the horizontal divider lines used as stylistic elements in the final table.
I did not know of an efficient way to properly right-justify the prices, so did some research and found helpful information on a forum posting at StackOverflow.com explaining how to do some simple text formatting using 'f-string' (
Format Output String, Right Alignment
, n.d.) Essentially, it allowed me to specify that I wanted the item names to be left-justified in a
column 30 spaces wide, then the prices right-justified in a column 10 spaces wide, in order to achieve the desired table layout.
References
Format output string, right alignment. (n.d.). Stack Overflow. https://stackoverflow.com/questions/8234445/format-output-string-right-alignment