Work out a price, with a discount and tax that are optional.
What to build
A function with defaults, called with however many values the input gives.
Requirements
- Write
final_price(price, discount=0, tax=5), where discount and tax are percentages. - Take off the discount first, and then add the tax to what is left.
- Read a line with one, two or three numbers, and print the final price with two decimals.
Input
One line with the price and, optionally, the discount and the tax.
Output
One number with two decimal places.
Constraints
The price is from 0 to 100000. The percentages are from 0 to 100.
Example
Input:
100
Output:
105.00
Why it works
Default values let the same function serve three kinds of call. The order of the steps matters: the tax is charged on the price after the discount.
Examples
100
105.00
100 10
94.50
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Give discount and tax default values in the function definition.
Hint 2
Pass only as many arguments as the input line provides. Remember that *values spreads a list into separate arguments.
Hint 3
Apply the discount first, then add the tax.
Solution
Show one correct solution (try it yourself first)
def final_price(price, discount=0, tax=5):
after_discount = price * (1 - discount / 100)
return after_discount * (1 + tax / 100)
values = [float(x) for x in input().split()]
print(f"{final_price(*values):.2f}")