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

Working on problem set 1 > problem 2. "Paying Debt Off in a Year." Everything seems pretty straightforward to me, but the program won't run in IDLE. After I enter the first two floats (initialBalance and interestRate), it just stops altogether, leaving me with a blinking cursor. I copied everything verbatim from the solution document (ps1b_solution.py], except I customized all my variables just for fun. And the solution document works perfectly in IDLE. Any idea what it means when the program just stops in its tracks? No red text, no nothing. Just a blinking cursor. Thanks!

OpenStudy (anonymous):

When it says it stops in its tracks it means that the program froze or quit.

OpenStudy (anonymous):

Thanks : ) Definitely didn't freeze, just kind of resets, or something.

OpenStudy (anonymous):

Yeah and ur welcs

OpenStudy (anonymous):

I guess my question is, what is the solution? How do I find and repair the problem in my code, so that the program runs? I tried opening stack viewer but that crashes the software every time.

OpenStudy (anonymous):

Never use stack viewer for sure.Lol Umm i dont know sorry

OpenStudy (anonymous):

Haha don't worry about it : )

OpenStudy (anonymous):

I gave you the medal since i didn't klnow it. Hope someone is can!

OpenStudy (anonymous):

My very first medal!!! > O

OpenStudy (anonymous):

Omg. Not my fisrt. Thx for medal btw. I am known at my city for technician even though I am 13.

OpenStudy (anonymous):

I believe that this means your program is still running, it might not be exiting the loop. You can use print statements to see where the problem is. If you copy and paste your code here I'll help you debug it

OpenStudy (anonymous):

Thanks! Balance = float(raw_input("Please Type Balance: ")) InterestRate = float(raw_input("Please Type Interest Rate as a Decimal: ")) MonthlyPayment = 0 MonthlyInterestRate = InterestRate/12 Newbalance = Balance while Balance > 0: MonthlyPayment += 10 Newbalance = Balance NumMonths = 0 while NumMonths < 12 and Newbalance > 0: NumMonths += 1 Interest = MonthlyInterestRate * Newbalance Newbalance -= MonthlyPayment Newbalance += Interest Newbalance = round(Newbalance,2) print "Result" print "Monthly Payment to Pay Off Debt in 1 Year:", MonthlyPayment print "Number of Months Needed: ", NumMonths print "Balance:", Newbalance

OpenStudy (anonymous):

as far as i can tell your balance is always greater than 0 so you are not exciting the first loop.

OpenStudy (anonymous):

I'm sorry, I think you are changing the balance, I am just not used to the operators you are using

OpenStudy (anonymous):

Below are modifications I did o your code, I got rid of the first loop because I dont think you need it. Also I declare NumMonths since it was not declared. I did not check for any other errors, I think you can take it from here. Balance = float(raw_input("Please Type Balance: ")) InterestRate = float(raw_input("Please Type Interest Rate as a Decimal: ")) MonthlyPayment = 0 MonthlyInterestRate = InterestRate/12 Newbalance = Balance NumMonths=0 ##while Balance > 0: ## ## MonthlyPayment += 10 ## Newbalance = Balance ## NumMonths = 0 while NumMonths < 12 and Newbalance > 0: NumMonths += 1 Interest = MonthlyInterestRate * Newbalance Newbalance -= MonthlyPayment Newbalance += Interest MonthlyPayment += 10 Newbalance = round(Newbalance,2) print "Result" print "Monthly Payment to Pay Off Debt in 1 Year:", MonthlyPayment print "Number of Months Needed: ", NumMonths print "Balance:", Newbalance

OpenStudy (anonymous):

Here's the original:

OpenStudy (anonymous):

your loop runs forever because your while statement is "while balance>0 but you are not changing balance, you are only changing Newbalance therefore your first loop never stops. Also you have numonths= 0 in the first loop therefore it makes your second loop never stop either since the condition is while numonths<12 and newbalance>0. Also you are not really changing NewBalance either since you reset it back to balance in your first loop. this is why it freezes when you run it, the loops are not stopping. - Remember, you have to take one payment for 12 months , add the compounded interest each month and at the end of 12 months if your balance is greater than zero, than you have to try another payment or in this case add 10 to the payment. I found that the best way to do this lab was to have a while loop with the condition being "while numberofmonths <12 : this gives you the ability to try one payment for 12 months. Under this while loop you have to update your balance monthly because of the compounded interest and subtraction of your monthly payment. Now under this loop you put an IF and statement that will let you increase the payment and reset the balance and month after 12 months if balance is greater than 0 . my statement reads " if newbal>0 and month==12.

OpenStudy (anonymous):

Ricardo - Thank you so much for the time and attention you've put into my question. I'm glad I joined this group, I can tell it's going to be very helpful. : ) Were you able to open the .py file I attached? That's the original solution code, and it runs perfectly in IDLE. All I did with my code was re-type the solution code verbatim, but I changed the variables. So I'm confused why their code runs and mine doesn't.

OpenStudy (anonymous):

I am so sorry, I gave you wrong information about the loops, I was not sure how they worked when they were nested, but this served as good practice. The only problem with you code is that I think you got a little confused when changing the names, on the first while loop it should be "while Newbalance>0" not "while balance>0 " as you have it. Balance is the raw input and you never change it therefore the loop will never stop, what you do make changes to is Newbalance. I tried it and its good now. Again, sorry for the misinformation earlier.

OpenStudy (anonymous):

Aha!!! Great. Problem solved. No need to apologize, because my main confusion was regarding what exactly was happening in IDLE, that is, why did the program just flat-out cease, with no error message? And you answered that question: I had messed up a variable, and the program was never able to exit the while loop. I sure wish that IDLE would give a little bit more information as to what's going on, instead of just leaving us with a blinky cursor and our thoughts. I wish I could find out what was happening with the stack viewer, which always crashes the software. I also wish it allowed you to change the font size from '6.' I also wish I was a famous guitarist. I digress. PS I thought that if I stated Balance = NewBalance, they would be interchangeable from then on out. This was a great lesson as to what "=" can and cannot do. Success! Thank you!

OpenStudy (anonymous):

Finally figure it out?

OpenStudy (anonymous):

Nipped it in the bud : )

OpenStudy (anonymous):

Days later...

OpenStudy (anonymous):

One day later. Barely. RicardoGarcia was a great help.

OpenStudy (anonymous):

@RicardoGarcia

OpenStudy (anonymous):

Interestingly enough, at the beginning of Unit 1 Lecture 4, "Machine Interruption of a Program," the professor covers how to find mistakes in your code. With his example, it was because there was a loophole around the decrementing function of a working While loop so that the program would never end if a certain decimal was entered. I'm not sure if his solution would have applied to my problem—a completely non-working while loop— but it's interesting nonetheless. The key is to insert print commands throughout, asking for definitions of variables, so you can see what's going on in the loop, behind the scenes.

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!