Python Forum

Full Version: I don't think my program is producing the correct answer
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey, I'm pretty new to python so the answer to this might be obvious, but I made this code for an assignment, and while the code is working I don't think the final answer it's producing is correct. Here is my code:

annual_salary = float(input("Enter your annual salary"))
total_cost = float(input("Enter the cost of the home"))
percent_saved = float(input("Enter the percent saved"))
current_savings = 0
months = 0
r = 0.04
portion_saved = percent_saved * annual_salary/12
down_payment = total_cost*0.25
while current_savings<down_payment:
    current_savings+=(portion_saved + current_savings*r/12)
    months+=1
print("The amount of months it will take to afford the down payment on the house is:",months)
Here is the result of running the code:
Enter your annual salary120000
Enter the cost of the home1000000
Enter the percent saved.1
The amount of months it will take to afford the down payment on the house is: 183
When I look at an example it seems that my result (183 months) is too high, however my code seems to me as if it should be correct. Again I'm sorry if the answer is obvious but I don't have much experience.
So, what's the correct answer? When dealing with calculations, you have to do the calculations yourself and compare. You can add print() calls after each calculation to display each value and compare that to your calculations. That way, you can determine what calculation (if any) is incorrect and debug it.

As you get farther along into your studies, you'll hear about testing and unit testing, etc. These are the same as the above. Effectively, you give the program inputs and programatically compare its outputs to the expected/correct outputs. It's just part of programming.

On a side note, the Python tags for the forum use square brackets.