LANGUAGE=PYTHON
Tip Calculator My code to calculate a tip is not working. The
calculateTip function’s comment describes the desired
functionality, but my code is not passing the test case. Try to
find my error and see if you can get these test cases to pass.
PYTHON.PY
def calculateTip(billTotal, tipPercent, roundup=False):
'''
Compute the tip based on the provided amount and percentage.
If the roundUp flag is set,
the tip should round the total up to the next full dollar
amount.
billTotal - total of the purchase
percentage - the percentage tip (e.g., .2 = 20%, 1 =
100%)
roundUp - if true, add to the tip so the billTotal +
tip is an even dollar amount
returns the calculated tip
'''
tip = billTotal * tipPercent
if roundup:
tipExtra = int((tip + 1.0)) - tip
tip = tip + tipExtra
return tip
#==================================================================================================
# Testing code below
def testBasicTip():
results = ""
TESTS = [(20, 0.15, 3.0), (100, 0.001, 0.1), (10, 1,
10)]
for test in TESTS:
actual = calculateTip(test[0],
test[1])
expectedHigh = test[2] + 0.01
expectedLow = test[2] -
0.01
if actual <= expectedLow or actual
>= expectedHigh:
results += "Basic Test
Failed: for a total of " + str(test[0]) + " and percentage " +
str(test[1]) + ", I expected " + str(test[2]) + " but your code
returned " + str(actual) + "\n"
return results
def testRoundupTip():
results = ""
TESTS = [(10.14, 0.15, 1.85), (100.01, 0.001, 0.99),
(20.01, 0.15, 3.99)]
for test in TESTS:
actual = calculateTip(test[0], test[1],
True)
expectedHigh = test[2] + 0.01
expectedLow = test[2] -
0.01
if actual <= expectedLow or actual
>= expectedHigh:
results += "Roundup Test
Failed: for a total of " + str(test[0]) + " and percentage " +
str(test[1]) + ", I expected " + str(test[2]) + " but your code
returned " + str(actual) + "\n"
return results
result = ""
result += testBasicTip()
result += testRoundupTip()
if len(result) == 0:
print("All Tests Passed!")
else:
print(result)
LANGUAGE=PYTHON Tip Calculator My code to calculate a tip is not working. The calculateTip function’s comment describes
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am