Skip to content

Instantly share code, notes, and snippets.

@TomKearney
Last active April 28, 2020 19:18
Show Gist options
  • Select an option

  • Save TomKearney/1d7caf09fe63f84a8949498f7123aa67 to your computer and use it in GitHub Desktop.

Select an option

Save TomKearney/1d7caf09fe63f84a8949498f7123aa67 to your computer and use it in GitHub Desktop.
Savings calculator
annual_salary = int(input("Enter your annual salary: "))
percentage_to_save = float(input("Enter the % of your salary to save (eg for 10% enter 10): ") / 100
total_cost = int(input("Enter the cost of your dream home: "))
def calculate_savings_timeframe(salary, percentage_to_save, total_cost):
down_payment_target = total_cost * 0.25
savings_rate = float(0.04)
months = total_saved = 0
while total_saved <= down_payment_target:
months += 1
total_saved += calc_monthly_saving(salary, percentage_to_save, total_saved, savings_rate)
print(f'Total amount saved after {months} months is {total_saved}')
return months
def calc_monthly_saving(salary, percentage_to_save, saved_so_far, savings_rate):
interest_earned = saved_so_far * savings_rate / 12
additional_savings = salary * percentage_to_save / 12
return interest_earned + additional_savings
savings_timeframe_months = calculate_savings_timeframe(annual_salary, percentage_to_save, total_cost)
print("Months required to achieve savings goal", savings_timeframe_months)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment