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

Problem 1b: So, I am strugging to figure out how to compute the fixed minimum monthly payment, number of months (at most 12 and possibly less than 12) it takes to pay off the debt, and the balance (likely to be a negative number). I correctly did 1a. But kind of stuck here. I am thinking I will need to use the while loop. and conditional statements.. What to do? Thanks! This is what I got so far: x = float(raw_input("Enter the outstanding balance on your credit card: ")) y = float(raw_input("Enter the annual credit card interest rate as a decimal: ") minmonthly_interestrate = y / 12.0

OpenStudy (anonymous):

x = float(raw_input("Enter the outstanding balance on your credit card: ")) y = float(raw_input("Enter the annual credit card interest rate as a decimal: ") minPayment = 10 newBalance = x while x > 0: for month in range(1, 13): newBalance = newBalance * (1 + (y/12))-minPayment if newBalance <= 0: break if newBalance <= 0: x = newBalance else: newBalance = x minPayment = minPayment + 10 print 'RESULT' print 'Monthly payment to pay off debt in 1 year: ' + str(minPayment) print 'Number of months needed: ' + str(month) print 'Balance: ' + str(round(x, 2))

OpenStudy (anonymous):

Let me explain it a little bit just so you understand what I am doing and why I am doing it a little better. You got the first part perfect, except rather than using x and y I would use some type of word like balance and interest. For a small program x and y may work, but once the program expands trying to remember x and y and what they do could get very difficult. So, monthly payments have to be in increments of $10, so that would mean the minimum payment can only be $10 a month. So I first start off my creating a minPayment variable that will hold 10 because that is the smallest payment possible. Once you start doing calculations, you will need a variable to store the end results of the balance. So I then created a variable that will hold the newBalance, and within that variable x (balance) will be stored for right now. You were right, you need to use conditional statements and a while loop. While loops are very helpful when progamming because they used when you need something to go until it meets a certain condition. So, in this case you need the program to keep going while x is greater than 0, once it is less than 0, this while loop will terminate. We are using 0 because we do not want the balance or anything to get below zero. Now, we use for statement, and we use for month in range (1, 13) this is creating a month variable, and it will go while it is in the range 1, 13. So it will start at the first month and go until the 12 month (December). Now, the newBalance variable gets told what it needs to do. This is your calculation to determine the interest rate. So, newBalance = newBalance * (1 + (y/12))-minPayment. I think you understand all of this seeing how you already had most of it. Now time for conditional statements. The if statement will tell the program that if the newBalance is less than or equal to zero to break, in other words the if statement will terminate. Nothing will happen, and in this case it will just move to the next if statement. Which just states that if the newBalance is less than or equal to zero, that it is going to set x equal to the newBalance. We need to update our balance in order to keep everything up to date. We did an equation on it, so we should store the end result within the newBalance (hint, new balance)!. Now, if it is not less than or equal to zero, it will move to the else statement which sets the newBalance = x. So the newBalance will have the value of your x statement (balance). And then it will tell your minPayment that it is equal to itself plus 10 because your monthly statements have to be increments of $10. Now, to the very end, you will see the following: print 'RESULT' print 'Monthly payment to pay off debt in 1 year: ' + str(minPayment) print 'Number of months needed: ' + str(month) print 'Balance: ' + str(round(x, 2)) The print 'RESULT' will simply just print "RESULT" on the top of the screen so that the user will know they are viewing the results of the operations. Now, you see print 'Monthly payment to pay off debt in 1 year: ' + str(minPayment), this will just print "Monthly payment to pay off debt in 1 year: " to the screen. The str(minPayment) is taking the minPayment variable and converting it to a string. And then the result will be displayed after the : on the screen. The print 'Number of months needed: ' + str(month) does the same things I just described, but this time it will convert the month variable into a string and then post it to the screen. Now, the last one says print 'Balance: ' + str(round(x,2)) which is just printing "Balance: " to the screen and then it takes your x variable (balance) and it is rounding it to two decimal places which is the normal rounding anyways. And then it prints it to the screen. You can change any of this you want, hopefully it helps you. But if you do need help, please just let me know and I will help you! I have not tried it in a compiler, so if there are any errors just try to fix them or contact me and I will help you.

OpenStudy (anonymous):

Ok, thanks for englightening me on this. Makes good sense. Why does minPayment = 10 newBalance = x draw the response invalid syntax?

OpenStudy (anonymous):

Thank you so much Andy! I had basically all the code you had (except the range which makes it look cleaner) but I wasn't using the break statement correctly and ending up in a neverending loop. So frustrating! Thanks for this pedagogical answer and explanation.

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!