Reputation: 33
Actually I am used to c++ and I am stuck in python. I can't seem to understand what's causing the infinite while loop. The main goal of the code is to calculate how many months are required to save enough money for upfront payment.
#*******Initializing all the required variables***********
home_price = float(input("Enter the price of your dream home:")) # cost of the home
down_payment_portion = 0.25 # initial upfront pay for the home which is 25%
stamp_duty_portion = 0.03 # 3%
annual_salary = float(input("Enter your annual salary:"))
tax_portion = 0.2 # 20%
save_amount = 0
save_portion = float(input("Enter the amount of money you want to save after tax-cut:"))
annual_return = 0.05 # 5%
months = 0
#*********************************************************
#------------Calculating all the necessary values---------
down_payment_portion = down_payment_portion * home_price # calculating the down payment for the dream home
stamp_duty_portion = stamp_duty_portion * home_price # calculating the stamp duty for the home
tax_portion = tax_portion * annual_salary # calculating the tax cut
save_portion = save_portion * save_amount # calculating the portion of tax-cut income to be put into savings
annual_return = (save_amount * annual_return) / 12
upfront_payment = down_payment_portion + stamp_duty_portion
while(save_amount < upfront_payment):
save_amount = save_amount + annual_return
save_amount = save_amount + (annual_salary - tax_portion) * save_portion / 12
months = months + 1
print(f'You will need {months} month to save enough for your upfront payment{upfront_payment}.')
Upvotes: 1
Views: 74
Reputation: 2129
save_amount = 0
annual_return
as annual_return = (save_amount * annual_return) / 12
which would be zerosave_portion
as save_portion = save_portion * save_amount
which is again zerosave_amount
in the while loop is never incremented it too remains zero, save_amount < upfront_payment
is always true ==> Infinite loopIn your code
while(save_amount < upfront_payment):
save_amount = save_amount + annual_return # 0 + 0 = 0
# 0 + (some value * 0)/12 = 0
save_amount = save_amount + (annual_salary - tax_portion) * save_portion / 12
Upvotes: 1