Python Forum
Trying to teach myself how to code and I'm stuck on a question I found online...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to teach myself how to code and I'm stuck on a question I found online...
#1
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-e...16_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.")
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 5 3,695 Nov-23-2020, 05:15 PM
Last Post: cananb
  Need help with this code and am stuck TheBoonester 2 2,251 Sep-19-2019, 07:56 PM
Last Post: j.crater
  I have homework that I've made no effort to show any code that I'm stuck on Ronaldinho 1 2,347 May-30-2019, 07:18 PM
Last Post: nilamo
  stuck in a block of code fadi 1 2,876 Aug-26-2017, 02:26 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020