Python Forum
Trying to teach myself how to code and I'm stuck on a question I found online... - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Trying to teach myself how to code and I'm stuck on a question I found online... (/thread-14770.html)



Trying to teach myself how to code and I'm stuck on a question I found online... - abushaa4 - Dec-16-2018

I found a sample question where I have to calculate how long it will take someone to save for a down payment for a mortgage (https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/assignments/MIT6_0001F16_ps1.pdf).

The part that I'm stuck on is where I have to increase wages every 6 months by a percentage. For some reason, the code that I have written doesn't increase wages every 6 months. Please see the link above for the question (part B: saving with a raise) and the code below for my attempt.

Any help will be greatly appreciated

total_cost = float(input("Enter the price of the house: "))
portion_down_payment = 0.25
current_savings = 0
apr = 0.04
semiannual_raise = (1 + float(input("Enter your semi annual raise: ")))
r = apr/12
annual_salary = float(input("Enter your annual salary: "))
portion_saved = float(input("Enter the portion you save: "))
monthly_salary = (annual_salary/12)*1
monthly_saved = monthly_salary*portion_saved
month = 0 
down_payment = total_cost*portion_down_payment



while down_payment > current_savings:
     if month % 6 == 0:
        annual_salary = annual_salary*semiannual_raise
     current_savings = current_savings + monthly_saved
     compound = (1 + r)
     current_savings = current_savings*compound
     month += 1

   
    


years = month/12 
 
print("Amount saved per month: " + str(monthly_saved))
print("Total amount saved: " + str(current_savings))
print("Total time taken: " + str(month) + " months or " + str(years) + " years.")



RE: Trying to teach myself how to code and I'm stuck on a question I found online... - ichabod801 - Dec-16-2018

You are adjusting the annual_salary, but when you do so you need to recalculate the values based on annual_salary (monthly_salary and monthly_saved). Generally changing a value in Python does not change other values based on the first value. This isn't a hard and fast rule, as certain operations with mutable objects (including lists, dictionarys, sets, and user define objects) can be affected. But for immutable objects (including ints, floats, strings, and tuples) changing the value of a variable has no effect on previous calculations based on that variable.