30 Days of Code in HackerRank with Python (Day 2: Operators)

Saptashwa Banerjee
2 min readJul 3, 2020

--

Objective
In this challenge, you’ll work with arithmetic operators. Check out the Tutorial tab for learning materials and an instructional video!

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.

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

Sample Input

12.00
20
8

Sample Output

15
Initial code for Day 2

Explanation

Now in Day 2 we have to find the total cost of meal including tax and tip so here we took 3 user input following meal_cost, tip_percentage, tax_percentage. But the point to be noted that the meal_cost is taken in the float format.
meal_cost = float(input())

tip_percent = int(input())

tax_percent = int(input())

Now we called a function named solve and pass the three parameters meal_cost, tip_percentage, tax_percentage

tip = tip_percent * meal_cost / 100

tax = tax_percent * meal_cost / 100

In the function part we just used simple percentage formula to figure out the amount of money for tax and tips. And finally in the print statement we print the sum of the meal, tax and tip.

Note: If there is no print statement in the main function then we directly print the output in the function and then there is no need of any return statement.

Now initially I mentioned we take the value of meal_cost in float
meal_cost = float(input()), So this sort of error occurred ! Hey wait it’s easy to overcome it!
So to figure it out we will use the round() function which basically ignores the decimal value and gives a integer output.

Now it will run Perfectly!

I hope you guys understood what the round function does, so see you in Day 3.

--

--