Input a total dinner amount (ex. $55.40) and the number of guest. The function should calculate the total cost (including 15% tip) and as equally as possible distribute the cost of the entire meal between the number of provided guests. (e.g., splitTip (15.16, 3) ==> guest1-$5.06, guest2-$5.05, guest3-$5.05). Whatever logic is used for uneven amounts should be deterministic and testable (all values rounded to cents - 2 decimal places).

Respuesta :

Answer:

  1. def splitTip(totalCost, numGuest):
  2.    finalCost = totalCost * 0.15 + totalCost  
  3.    avgCost = finalCost / numGuest  
  4.    for i in range(numGuest):
  5.        print("Guest " + str(i+1) + ": $" + str(round(avgCost,2)))
  6. splitTip(15.16,3)

Explanation:

The solution is written in Python 3.

To calculate the average cost shared by the guests, we create a function that takes two input, totalCost and numGuest (Line 1). Next apply the calculation formula to calculate the final cost after adding the 15% tips (Line 2). Next calculate the average cost shared by each guest by dividing the final cost by number of guest (Line 3). At last, use a for loop to print out the cost borne by each guest (Line 4-5).  

ACCESS MORE