Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 10 Online
OpenStudy (anonymous):

Question about assignment 1 part 3 I've come up with the following: balance= float(input('initial balance:')) interestrate= float(input('annual interest rate:')) low= balance/12 high= balance*(1+interestrate)/12 payment= (high + low)/2.0 numMonths = 0 while payment*12 - balance != 0: if payment*12 < balance: low = payment else: high = payment

OpenStudy (anonymous):

while numMonths <= 12 and balance > 0: interest = balance*interestrate/12 principlerepayment = (payment - balance*interestrate/12) balance -= principlerepayment numMonths += 1 print ("Month:", numMonths) print ('Interest payment:', interest) print ('Remaining balance:', balance) print ('payment', payment) The trouble seems to be I overshoot the number of months slightly and don't get a round answer. Any help understanding why this happens would be appreciated.

OpenStudy (anonymous):

You should not do equals comparisons with floats or doubles. You should always be doing >, <, >=, <= comparisons.

OpenStudy (anonymous):

I see your point and that's probably good practice generally. But I did use a more complex statement earlier with such conditions and it doesn't seem to make a difference to the problem I'm having.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!