BaşlayınÜcretsiz Başlayın

Calculating a discount

You're building a feature that calculates discounted prices for an online store. You want to be able to set different discount amounts and control whether prices are rounded to look cleaner in the app.

In this exercise, you will create a custom function that handles these requirements using default and keyword arguments.

Bu egzersiz

Intermediate Python for Developers

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Define the calculate_discount() function with default arguments for discount_percent(15) and round_result(True).
  • Round the result to two decimal places inside the if statement.
  • Call the function, setting discount_percent to 25 and round_result to False.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

original_price = 899.99

# Define the function with default arguments
def calculate_discount(price, ____=15, ____=True):
    discounted_price = price - (price * (discount_percent / 100))
    
    if round_result == True:
        # Round the result to two decimal places
        return ____(discounted_price, ____)
    else:
        return discounted_price

# Call the function with keyword arguments
final_price = calculate_discount(price=original_price, ____=___, ____=____)
print(final_price)
Kodu Düzenle ve Çalıştır