curriculum/challenges/english/blocks/lab-discount-calculator/695774002591bbc5f8cf3e53.md
In this lab you will write a function that calculates the final price of an item after applying a percentage discount.
For example, if the price of an item is 50 and a discount of 20 is applied, the discount amount is 10, and the final price is 40.
Objective: Fulfill the user stories below to complete the lab.
User Stories:
You should define a function named apply_discount.
The apply_discount function should take exactly two parameters: price and discount.
If price is not a number (int or float), the function should return the string The price should be a number.
If discount is not a number (int or float), the function should return the string The discount should be a number.
If price is less than or equal to 0, the function should return the string The price should be greater than 0.
If discount is less than 0 or greater than 100, the function should return the string The discount should be between 0 and 100.
If both inputs are valid, the function should calculate the discount as a percentage of the price.
The function should return the final price after applying the discount.
You should have a function named apply_discount.
({
test: () => runPython(
`assert _Node(_code).has_function("apply_discount")`
)
})
Your apply_discount function should take two parameters: price and discount.
({
test: () => runPython(
`
func = _Node(_code).find_function("apply_discount")
assert func.has_args("price, discount")
`
)
})
When apply_discount is called with a price (first argument) that is not a number (int or float) it should return The price should be a number.
({
test: () => runPython(
`
wrong_values = ["HelloWorld", "100", [1,2], {"a":1}, (1,2), None]
for v in wrong_values:
assert apply_discount(v, 10) == "The price should be a number"
`
)
})
When apply_discount is called with a discount (second argument) that is not a number (int or float) it should return The discount should be a number.
({
test: () => runPython(
`
wrong_values = ["HelloWorld", "10", [1], {"a":1}, (1,), None]
for v in wrong_values:
assert apply_discount(100, v) == "The discount should be a number"
`
)
})
When apply_discount is called with a price lower than or equal to 0, it should return The price should be greater than 0.
({
test: () => runPython(
`
assert apply_discount(0, 10) == "The price should be greater than 0"
assert apply_discount(-5, 10) == "The price should be greater than 0"
`
)
})
When apply_discount is called with a discount lower than 0 or greater than 100, it should return The discount should be between 0 and 100.
({
test: () => runPython(
`
assert apply_discount(100, -1) == "The discount should be between 0 and 100"
assert apply_discount(100, 101) == "The discount should be between 0 and 100"
`
)
})
apply_discount(100, 20) should return 80.
({
test: () => runPython(
`
assert apply_discount(100, 20) == 80
`
)
})
apply_discount(200, 50) should return 100.
({
test: () => runPython(
`
assert apply_discount(200, 50) == 100
`
)
})
apply_discount(50, 0) should return 50.
({
test: () => runPython(
`
assert apply_discount(50, 0) == 50
`
)
})
When apply_discount is called with a discount of 100, it should return 0.
({
test: () => runPython(
`
assert apply_discount(120, 100) == 0
`
)
})
apply_discount(74.5, 20.0) should return 59.6.
({
test: () => runPython(
`
result = apply_discount(74.5, 20.0)
assert result == 59.6
`
)
})
def apply_discount(price, discount):
# Validation steps
if not isinstance(price, (int, float)):
return "The price should be a number"
if not isinstance(discount, (int, float)):
return "The discount should be a number"
if price <= 0:
return "The price should be greater than 0"
if discount < 0 or discount > 100:
return "The discount should be between 0 and 100"
discount_amount = price * discount / 100
final_price = price - discount_amount
return final_price