pythonbeginner10 minutes
Fix the Bug in Function to Calculate Total Price with Tax
A simple Python function is intended to calculate the total price of an item after adding tax. However, the function contains a bug causing incorrect results. Your task is to find and fix the bug so the function returns the correct total price.
Challenge prompt
The function calculate_total_price is supposed to take a price and a tax rate, then return the total price including tax. However, it currently returns the wrong value. Identify the bug in the code below and fix it so the function returns the correct total price including tax.
Guidance
- • Check the calculation logic inside the function carefully.
- • Remember that tax should be *added* to the original price, not subtracted or incorrectly multiplied.
- • Test your fixed function with different price and tax values.
Hints
- • Think about how to correctly calculate the total including tax: total = price + (price * tax_rate).
- • Check the operator precedence and parentheses in the expression.
- • Make sure the tax_rate is treated as a decimal (e.g., 0.1 for 10%).
Starter code
def calculate_total_price(price, tax_rate):
# This function should return price including tax
total = price * tax_rate + price
return total
# Example usage:
print(calculate_total_price(100, 0.1)) # Expected output: 110.0Expected output
110.0
Core concepts
basic arithmetic operationsfunction definitionsoperator precedencereturn statements
Challenge a Friend
Send this duel to someone else and see if they can solve it.