Write a function called futureRailFare that uses a for loop to calculate the future…
- User
Write a function called futureRailFare that uses a for loop to calculate the future value of a train ticket assuming a monthly price rise of 2%.
- Assistant
Here is one possible implementation of the function in Python:
def futureRailFare(initial_price, months): future_price = initial_price monthly_increase = 1.02 # 2% monthly price rise for month in range(months): future_price *= monthly_increase return future_priceIn this function,
initial_pricerepresents the current price of the train ticket, andmonthsis the number of months in the future for which we want to calculate the future value of the ticket.The function initializes
future_pricewith the initial price, and then uses a for loop to iterate over each month. Within each iteration, thefuture_priceis updated by multiplying it withmonthly_increase, which represents a 2% increase in price every month.After iterating for the specified number of
months, the function returns the finalfuture_price.