also...what is going on with this: I'm defining an equation (6*a)+(9*b)+(20*c) and it's independent variables (a=0, b=0, c=0) at the TOP of my program...I then run through a bunch of statements and then in the middle of my program I step the variables (by 1)...I do this by entering them again (further down where I am executing)...when I reference the equation (which I did NOT rewrite...it's still at the top) at the same place as the new variables the equation = 0?!?! It's as if it's referencing the old variables even though I've just updated them...when I move the equation down it works fine?
Are you saying you did something like this? a, b, c = 0, 0, 0 x = (6*a) + (9*b) + (20*c) a, b, c = 1, 1, 1 print x # Why is x still 0?!?! x isn't an equation, it's a value, which is set to the value of that expression at the time it was executed. If you want x to change you have to assign it a new value. In this case, you'd want to reevaluate the expression with the new values, then assign it to x. If this isn't what you did, then you should post your code so we can see what you're actually doing.
mpizzle: Post your code to codepad.org so we can see what's happening!
If what dmancine wrote is what is happening, your problem is on the bad understanding on how Python works on assignments. Whenever you state x = foo, what you are really saying is that x has a pointer to a reference that is the location of foo. Compare this with saying that x holds not a value, but a reference to a certain value. And whenever you say x = y, you are actually creating a new reference to the same value. All this is opposed to copying the value with the assignment operator, i.e., you can access a variable via a pointer, because it holds the actual value. I think that, basically, that is what happens, correct me anyone if I am wrong. Or if I need to explain any better. On one of the early lectures in this course, Prof. Grimson discussed the basics of this kind of behavior.
sorry, let me explain better (and post my code): http://codepad.org/BGDmHeRh Basically...you can see where 'equation' was at the top. All else being equal, when I tried to 'print equation' at the end I was getting 0 back. As soon as I moved 'equation' down to just above where I was calling it things started to work as expected... What is the practice on where in your code you name variables. Should something like 'equation' be at the top or is it fine to name it further down? Not sure why it wasn't working at the top...?
Again, I think you have to reassign the equation variable, like this: http://codepad.org/VtWVn9Mo . What is really happening is this:|dw:1318903580167:dw| ; that is, Eq. is actually pointing to the value that a,b,c had whenever Eq. was assigned first. Even if you change, a, b or c, Eq. doesn't know preemptively whether or not a,b or c changed. You have to do another assignment statement so it can check "What does these variables are pointing to?" "Oh, b,c are 2, I calculate these values now.". It won't do this check by itself. Hope that this explanation is clear enough.
Join our real-time social learning platform and learn together with your friends!