Could anyone help me with problem set 1, problem 2? I have the following solution which works fine for Test Case 1 but gives a completely wrong answer for Test Case 2. I cannot work out why. rawBalance = float(raw_input('Enter the outstanding balance on your credit card: ')) rawAIR = float(raw_input('Enter the annual credit card interest rate as a decimal: ')) Balance = round(rawBalance, 2) AIR = round(rawAIR, 2) MIR = AIR/12.0 def payoff(MMP): Balance = round(rawBalance, 2) AIR = round(rawAIR, 2) MIR = AIR/12.0 Month = 1 while Month<12: InterestPaid
I'm guessing your code got cut off at the end there?
Ah, apologies! Here is the rest (hopefully): while Month<12: InterestPaid = MIR*Balance PrincipalPaid = MMP - InterestPaid Balance -= PrincipalPaid Month +=1 if Balance>0: return (0,0,0) else: return (1,Month-1,Balance) for MMP in range(int(Balance*MIR)+1,int(Balance)): if payoff(MMP)[0]==1: print 'Monthly payment to pay off debt in 1 year: ',MMP print 'Number of months needed: ',payoff(MMP)[1] print 'Balance: ',round(payoff(MMP)[2], 2) break else: continue
pls use a code pasting site http://dpaste.com http://pastebin.com http://codepad.org
please correct your paste using any code pasting site then paste it correctky here
Correction: while Month<=12; as of now your function doesn't iterate for the 12th month. However, since your for program steps MMP by 1, the results might be slightly different, and your program will always end in 12 months as the MMP needed for 12 months will be lower than that needed for 11 months.
Piggy backing on what @atinisachan said your code doesn't iterate all 12 months. Also you should have some kind of break command in the case that the monthly can be reached in less than 12 months.
Join our real-time social learning platform and learn together with your friends!