Python Forum
I don't think my program is producing the correct answer
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I don't think my program is producing the correct answer
#1
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.
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Filter and str.isdigit producing an error tester_V 5 1,853 Aug-12-2022, 07:50 AM
Last Post: Gribouillis
  pyautogui.locateOnScreen producing error dude8074 6 3,735 Apr-17-2022, 05:05 PM
Last Post: bowlofred
  producing numbers out of a list bouraque7878 10 3,625 Nov-12-2021, 09:13 PM
Last Post: jefsummers
  why don't i get the answer i want CompleteNewb 12 3,296 Sep-04-2021, 03:59 PM
Last Post: CompleteNewb
  I am getting the wrong answer, and not sure why riskeay 3 2,000 Nov-05-2020, 08:24 PM
Last Post: deanhystad
  Make the answer of input int and str enderfran2006 2 1,970 Oct-12-2020, 09:44 AM
Last Post: DeaD_EyE
  [Beginner] Code is not producing desired result fakej171 2 2,380 Mar-21-2020, 10:26 AM
Last Post: buran
  user input producing incorrect boolean al_Czervik 4 3,023 Mar-05-2020, 09:50 PM
Last Post: al_Czervik
  Keeps looping even after correct answer mcesmcsc 2 1,871 Dec-12-2019, 04:27 PM
Last Post: mcesmcsc
  I'm getting a wrong answer don't know where the bug is 357mag 4 2,763 Jul-07-2019, 11:21 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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