Here's the question you clicked on:
JocelynC
Why am I so bad at programming D: I'm doing something wrong on problem 3: 1. I know something in wrong in my decrementing function, but I'm not sure what. I think it has to do with the second part of the function. 2. I'm a little confused on how to figure out how many months it will take. It seems easy, but I'm drawing a blank...
THE ATTEMPT: ##PROBLEM III ##min monthly payment needed to pay of debt in a year x = float(raw_input('Enter Current Balance ')) y = float(raw_input('Enter annual credit card interest decimal ')) epsilon = 0.01 numGuesses = 0 low = x/12 high = (x*(1+(y/12))**12)/12 ans = (high + low)/2.0 while abs((ans*12)-x) >= epsilon and ans <= x/12: print low, high, ans numGuesses += 1 if x/12 < x: low = ans else: high = ans ans = (high + low)/2.0 #Finding months needed to pay d = ans*12 - x m = 0 while d > m * ans: (///gets lost and gives up///) print 'numGuesses =', numGuesses print ans, 'is close to minumun balance when you have', x, 'as your balance'
You're not bad at programming, you're learning. Looking over your code, you have several lines that are very complex and can lead to unexpected results if you're not spot on with operator precedents (which operations are done in which order). If you force yourself to write more comments, it will help you section your code, so that one stanza/block does one specific task and has certain inputs and outputs, which make debugging easier. I've started to write my comments before I start coding, so that I can describe what exactly I expect a function/method to do. Kind of a substitute for pseudocode. Variable names that really describe what that variable contains make the code much more natural for people to read. This helps debugging too. As you can guess, I screw up on a regular basis, so I'm interested in cutting that down.
Hey JocelynC, don't be discouraged. I'm new to programming as well, and these problems took me a while to figure out. So far, I can't figure out problem C, and have been trying to figure it out so I could try answering your question. Just remember, like the professor said... keep reading code, keep debugging it, keep running it, etc. Practice makes perfect. This problem is tricky, and I'm still trying to grasp how to correctly run a bisection search. For now, we don't need var numGuesses. Also, we will need a for loop with range so we can keep the answer within 12 months.
I guess I'll put in more comments... and watch more lectures...I've only been up to number 3... Is there an error in the second part of the decrementing statement? ("and ans <= x/12:") I know it's that part that's likely messing me up.
Yes, I think the second condition on your while statement won't do anything since you are working with a bottom range 'low'.. So in the code right now, you are only using the first condition.
Agh. Even when I change it to the "high" it just runs continuously... 0.0
I know we need to change if x/12 < x: Just haven't figured out what to change it to
Is that the "Minimum monthly payment" problem?? if so, do not overthink the problem, look you even said some key, on your message: "##min monthly payment needed to pay of debt in a year" with that you know it has to run 12 times, I think it asks you to give the totalpaid and the remaining balance, with that you know the remaining balance will change every month, and the totalpaid will increase ... I hope it'll be that problem, cuz if not, I'm writing nonsense, be patient and try to always refresh your mind, you can do it!
In the example, though, it had the debt getting paid off in 11 months.... Yes, this is the "Minimum monthly payment problem." By running continously, the loop reaches a point where the number stops changing completely, and prints out the same thing continously in a never-ending loop. By that, the "print low, mid, high" all are the same number and the program keeps on printing that number. Is my process right and the loop drawn incorrectly, or both? D:
ok let me run your code first :B ... didn't test it :$
Seeing your code, the variable which gonna make stop in some time the while loop is: "ans", so you have to keep changing "ans", but! your if clause says: -- if x/12 < x: low = ans else: high = ans ans = (high + low)/2.0 -- x/12 always is gonna be less than x, so else will never run, you only update 'low' and just matter of time until low reach the value of 'ans' and 'high'. The fact you put outside of the while loop, the formula for high, doesn't mean that inside will change, it'll be better to take it inside, remember if you compare something with the balance - variable 'x'- you can't touch that value, cuz will change your comparison, just create a new variable to be updated, and that new balance will decrease, each month, until it gets to be lower than epsilon; and inside the comparison, with the formula try if your payment (low + high)/2 is enough for the 12 months to pay the whole balance, good luck! I hope this help you.
Should I change x/12< ans? Would that work?
no cuz is the same, you'll be trap on the same infinite loop, there's a variable you are not using, the diference between a new balance - got it from the minimunPayment - and the initial balance, should be epsilon.
I tried using epsilon to set the range, as in, how close I need the answer to be.
Let's say that you balance is $500 and with the minimumPayment you find out the balance you paid is $499; the difference is $1, but still greater than epsilon(0.01), so you'll need to find a liiiittle higher minimumPayment, so the balance difference will be lower.
Hm. I thought epsilon told the increments of the bisection search, as in how far was an acceptable answer.
But, now you say that, if you make an increment of epsilon (0.01) to find your answer ... if you start in 0 and the answer is 53 ... will be a lot of epsilons xD now you know is the difference between two values you want to compare (:
I'm actually still a little confused. I based the program off of the teacher's program: x = int(raw_input('enter a number ')) epsilon = 0.01 numGuesses = 0 low = 0.0 high = x ans = (high + low)/2.0 while abs(ans**2 - x) >= epsilon and ans <= x: #print low, high, ans numGuesses += 1 if ans**2 < x: low = ans else: high = ans ans = (high + low)/2.0 #print 'numGuesses =', numGuesses print ans, 'is close to square root of', x in this one, don't they use epsilon as how close to the answer the guess must be?
Yup, the example you posted uses bisection search. Epsilon is the variable used to determine how close the solution has to be until the loop ends. But, in the example provided, epsilon is not the number incremented. Instead, it is using variable ans to get the midway point. Then it is using the if statement: ans**2 < x to determine if the answer should be in the bottom half of the selection, or the top half.
Does anyone know how to outright fix my program and then tell me how it works? I've come to the eventual conclusion that I'll never find the answer independently (it's a never ending program! :p) or in any reasonable amount of time... /surrenders to fate
Haha is better to explain somethin in real time, cuz the aim is to encourage you to kill him (the code), I won't put it here, I'll send you a PM
PhilipY do not put code solution on here, instead use PM, remember this is a space for discussing and understanding a possible answer.
Ah ok sorry about that. I deleted the message and will pm them in the future