Python Functions for Tip Calculation and Car Payment
Function 1: calculate_tip
def calculate_tip(bill_amount: float, tip_percentage: float) -> float: ''' Calculate the tip amount based on the bill amount and tip percentage.
Inputs:
- bill_amount : float : the total bill amount in CNY
- tip_percentage : float : the tip percentage as a decimal
Output:
- float : the tip amount in CNY
Example:
calculate_tip(100, 0.15) => 15.0
calculate_tip(50, 0.2) => 10.0
calculate_tip(80, 0.18) => 14.4
This function calculates the tip amount by multiplying the bill amount by the tip percentage.
'''
tip_amount = bill_amount * tip_percentage
return tip_amount
Function 2: calculate_car_payment
def calculate_car_payment(principle: float, interest_rate: float, months: int) -> float: ''' Calculate the monthly car payment using the provided formula.
Inputs:
- principle : float : the principle amount in CNY
- interest_rate : float : the interest rate
- months : int : the number of months for payment
Output:
- float : the monthly car payment in CNY
Example:
calculate_car_payment(50000, 0.05, 36) => 1501.56
calculate_car_payment(80000, 0.08, 48) => 1952.30
calculate_car_payment(60000, 0.06, 60) => 1199.10
This function calculates the monthly car payment using the given formula:
P * (r/12) / (1 - (1 + r/12)^(-m))
'''
numerator = principle * (interest_rate / 12)
denominator = 1 - (1 + interest_rate / 12) ** (-months)
car_payment = numerator / denominator
return car_payment
原文地址: https://www.cveoy.top/t/topic/lTy8 著作权归作者所有。请勿转载和采集!