시작하기무료로 시작하기

할인 금액 계산하기

온라인 쇼핑몰에서 할인된 가격을 계산하는 기능을 만들고 있습니다. 다양한 할인율을 적용하고, 앱에서 보기 좋게 가격을 반올림할지도 제어할 수 있어야 합니다.

이번 연습 문제에서는 기본 인수와 키워드 인수를 활용해 이러한 요건을 구현하는 사용자 정의 함수를 만들어 보겠습니다.

이 연습은 강의의 일부입니다

개발자를 위한 Python 중급

강의 보기

연습 안내

  • discount_percent(15)와 round_result(True)에 기본 인수를 설정하여 calculate_discount() 함수를 정의하세요.
  • if 구문 내부에서 결과를 소수점 두 자리로 반올림하세요.
  • discount_percent25, round_resultFalse로 설정하여 함수를 호출하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

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)
코드 편집 및 실행