0

This is the third question i got from hackerrank's 30 days of code(https://www.hackerrank.com/challenges/30-operators/problem)

Task:

Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal's total cost.

Sample Input:

12.00

20

8

Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded result! Explanation

def solve(meal_cost, tip_percent, tax_percent):
    tip = (tip_percent/100) * meal_cost
    tax = (tax_percent/100) * meal_cost
    total_cost = meal_cost + tip + tax
    print(round(int(total_cost)))

Above code worked for most of the inputs given from the system expect 1 which was

10.25

17

5

my code prints 12 but the expected output is 13, my only guess is that the expected output is incorrect

  • 1
    For the input (10.25,17,5), the total cost is 12.504. When you apply int, it gives you the integer 12. That's why you don't obtain 13 as a result. – lauriane.g Oct 28 '20 at 09:19
  • 1
    You can find an explanation here : https://stackoverflow.com/a/31818069/14280520 – lauriane.g Oct 28 '20 at 09:23

1 Answers1

0

The problem is with the last line of your function definition. When you use int(total_cost), the value is truncated to be parsed into an integer. What you should do is round the value without truncating to an integer like so:

print(round(total_cost))

As a note, round also returns an integer, but rounded instead of truncated.

XPhyro
  • 400
  • 1
  • 5
  • 13