On problem set 1, I figured out the first question but I'm stuck on question two on how to configure the code for the loop. Here is what I have for question 2. If anyone could check it over and show me where my mistakes are then I would REALLY appreciate it. balance = float(raw_input('What is the balance on the credit card?')) annInterest = float(raw_input('What is the annual interest rate as decimal?')) monthPay = 10 month = 1 monthInterest = (annInterest / 12.0) while (balance > 0): print(balance,end="") month = month + 1 month < 13 monthPay = monthPay + 10
Coming from someone who can do problems 1 & 2, but not 3 (read: total beginner), you need some 'if' statements in your loop. Use two if statements in your loop. One to tell it what happens when balance < 0. Another to tell it what to do when balance > 0 and month = 12. In one case, you've reached your answer. In another, you need to reset and try again.
ok I'll try that thanks
how bout using a code pasting site to post your code?? http://dpaste.com http://pastebin.com http://codepad.org
Oh I'm new to this. I didn't realize that you could do that. I'll update my code with one of those sites if I get stuck again thanks!
I'm getting an answer now but its the wrong one according to the directions. Here is my updated code balance = float(raw_input('What is the balance on the credit card?')) annInterest = float(raw_input('What is the annual interest rate as decimal?')) monthPay = 10 month = 1 monthInterest = (annInterest / 12.0) while (balance > 0): month = month + 1 month < 13 monthPay = monthPay + 10 balance = balance * (1 + monthInterest) - monthPay if balance < 0: print 'Result' print 'Minimum monthly payment is' + str(monthPay) print 'The number of months needed is' + str(month) print 'Balance:' + str(balance)
oh and here is the link to the code... http://dpaste.com/771086/
The first thing I noticed is that your answer test case 2 ($32,000) was 210 months. The problem said that all payments have to be done within 12 months, so let's start there. Since you got 210 months, your loop is continuing to execute even after month 12. So we need to create code that stops the loop at 12 months. For one thing, you can add a conditional to your while statement to only run when month <= 12. (use an 'and' operator). I see your if statement, which is good. But you're missing one. What happens when the balance is greater than 0 AND you're at month 12? Then you need to add $10 to your guess and re-enter the loop. You can do that by setting month = 0 and resetting your balance to the original input. Sorry, I know I'm not articulating well, but hopefully this puts you one step closer. I'll check back tomorrow.
Ok thanks. I finally figured it out with your help along with an example of a code that works. I just wish I could figure it out before I look up the answer!
Join our real-time social learning platform and learn together with your friends!