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

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

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

ok I'll try that thanks

OpenStudy (anonymous):

how bout using a code pasting site to post your code?? http://dpaste.com http://pastebin.com http://codepad.org

OpenStudy (anonymous):

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!

OpenStudy (anonymous):

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)

OpenStudy (anonymous):

oh and here is the link to the code... http://dpaste.com/771086/

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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!

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!
Latest Questions
luvnickk: how do i use this ud83dude2d
50 seconds ago 6 Replies 0 Medals
luvnickk: am newwww
12 minutes ago 0 Replies 0 Medals
russianmafiya: can someone help me write a love song
54 minutes ago 1 Reply 0 Medals
velmalovesshaggy145: Isnu00b4t this the opening melody to mood by 24kgoldn feat iann dior? https://voca.
56 minutes ago 7 Replies 2 Medals
arrivhn: ADD ME ON DISCORD ICYAFFL
1 hour ago 4 Replies 1 Medal
arrivhn: whats is the accute of a cricle
1 hour ago 4 Replies 3 Medals
arrivhn: KNOCK KNOCK
1 hour ago 12 Replies 2 Medals
arrivhn: whats is 77 times 8
1 hour ago 8 Replies 1 Medal
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!