Question: *Python Question* Complete the lunchBill()function. This function takes a single list argument whose first element is a numerical value (the base price of my lunch

*Python Question*

Complete the lunchBill()function. This function takes a single list argument whose first element is a numerical value (the base price of my lunch order) and whose remaining elements (if any) are 2-element sublists that modify the base price as described below.

A sublist whose first element is the string "tip" means to add a tip of the given percentage (the second element) to the total so far. For example, the sublist ["tip", 25] means "apply a 25% tip" by multiplying the current bill amount by 1.25 (thus increasing it by 25%). To get the tip multiplier, add (tip amount / 100) to 1.0.

A sublist whose first element is the string "extra" means to add the specified amount (the second element) to the total so far. For example, the sublist ["extra", 5] means "add 5 to the current bill amount".

A sublist whose first element is the string "coupon" means to subtract the specified amount (the second element) from the total so far. For example, ["coupon", 3] means "subtract 3 from the current bill amount".

A sublist whose first element is the string "discount" means to subtract the specified percentage (the second element) from the total so far. For example, ["discount", 35] means to apply a 35% discount by multiplying the current bill amount by 0.65 (to get the discount multiplier, subtract (discount amount / 100) from 1).

The function argument may contain any number of tip, extra, coupon, and discount sublists, in any order. These modifiers MUST be applied in the same order that they appear in the main list. For each modifier, print a brief summary of the adjustment (e.g., "Adding a 20% tip"). The function should return the final bill amount, or 0 if the final amount is less than 0 (meaning that my lunch was free).

Your function should begin by examining the first element of the list (the base price). Use a for loop to examine any remaining elements of the list, from index 1 through the end. For each element, use an if-elif-else chain to process the four types of bill modifiers. You may assume that any sublists that you encounter will always begin with one of the four types described above (all lowercase), and will always feature non-negative values to add, subtract, or multiply by. Finally, at the end of the loop, use an if-else statement to determine what value to return.

Example 1:

The list [12.34, ["tip", 15], ["discount", 25], ["extra", 1.5]]

means that we start with a base price of $12.34. We then multiply by 1.15 to add the 15 percent tip, giving us $14.191 (don't worry about rounding for this problem). Next, we apply a 25 percent discount by multiplying by 0.75 to get $10.64325. Finally, we add an extra charge of 1.5 to the bill, for a final total of $12.14325.

Example 2:

The list[10, ["tip", 20], ["discount", 20], ["coupon", 2], ["coupon", 2], ["coupon", 1], ["discount", 50], ["coupon", 2.55]]

means that we start with a base price of $10. We then multiply by 1.20 to add a 20 percent tip, getting $12. Next, we multiply by 0.80 to apply a 20 percent discount, getting $9.6. We apply three coupons in turn, subtracting $2, $2, and then $1, to get $4.6. Applying another 50 percent discount gives us $2.3. Finally, we subtract $2.55 for one final coupon, giving us a final value of -$0.25. This value is less than 0, so the function will return.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!