anonymous
  • anonymous
my code is off by 10 compared to other test cases, but not all the time, any ideas? balance = 4157 annualInterestRate = 0.18 payment = 10 def payforyear(x): count = 0 while count < 12: x = (x - payment) + (x*(annualInterestRate / 12)) count = count + 1 if count == 12: return x while payforyear(balance) > 0: payment = payment + 10 else: print ('Lowest payment: ' +str(payment) )
MIT 6.00 Intro Computer Science (OCW)
katieb
  • katieb
See more answers at brainly.com
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this
and thousands of other questions

anonymous
  • anonymous
Why do you check if count == 12? Since the while loop will end anyway when the count reaches 12, you can just return the value!
anonymous
  • anonymous
if i recall correctly, if i didn't check count 12 and put the return x line in it returned the result after one iteration of the loop
anonymous
  • anonymous
Well, the main thing that is wrong is the formula (x-paymnet)+(x*(annualInterestRate/12)) If you check instructions it is (xipayment)*(1-(annualInterestRate/12)) This will fix your problem but stylistically using variables defined outside the function is bad practice unless absolutely necessary.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
Sorry just to be clear, which exercise are you trying to solve? I guess it's the second one!
anonymous
  • anonymous
That should read (x-payment)...
anonymous
  • anonymous
And (1+(... not minus
anonymous
  • anonymous
x = (x - payment) *(1+annualInterestRate / 12) should do the trick I guess
anonymous
  • anonymous
Yes, it does actually problem is I have no idea how, and why i got it wrong in the first place. I have looked at that area of the code more than a few times over the past 12 hours I have been working on it. I have this weird feeling that my solution is very very very far from other peoples solutions.
anonymous
  • anonymous
Yes and as chris said previously the if count==12 is not necessary as long as the return statement is lined up with the while, I tested it too.
anonymous
  • anonymous
yes thank you for that Chris, I am still getting my head around logic and python work flow. I was scratching my head with the while statement and indentation for a good couple of hours.
anonymous
  • anonymous
Indentation in python is very important to the meaning and this is a good reason for beginners to learn python because it forces good habits of indentation. Which isn't necessary in some languages but always recommended.
anonymous
  • anonymous
Ditto!

Looking for something else?

Not the answer you are looking for? Search for more explanations.