![]() |
A noob question on editing variables
OK I have just started to learn how to program in python yesterday and I have never programed before that, that being said please don't flame me to much if this is a really stupid question.
OK heres what I want to do, I have a button made using tkinter and when I click that button it runs a function(I have all that set up already), now what I want the function to do is to add a number to a global variable and have that variable keep that value,for example if the variable originally equals 0 then when I click the button the variable would equal 1 and if I click the button a second time the variable would equal 2 and so on. So how would I do that? thanks in advance |
variable = variable + 1
|
or the alternative that's in Java,
variable++ lol, sorry tempest, had to throw that in there! wait...this is a Python board...:o |
Quote:
UnboundLocalError: local variable 'num' referenced before assignment |
You first have to declare "num" with an initial value. If you are trying to add num to itself plus one, it must first have a value.
Try putting "num = 0" at the beginning of your program. As well, "num += 1" is a more well-formed alternative to "num = num + 1". |
When doing that I get this error:
UnboundLocalError: local variable 'num' referenced before assignment. Here is the whole code I am using: :
#!/usr/bin/pythonAs you can see it creates a window, a entry box, and three buttons. First you click th "open" button to create a text file , then you enter something in the entry box and press "insert", what was in the entry box is added to the text file along with the value of the variable I'm having a problem with and another string, when your done adding things to the text file you press "close" to close the file and that all there is to it |
Quote:
|
Also, you might actually pay attention to the responses you have. You also need to read the forum's rules/FAQ and learn about code tags.
|
^^^Sorry I was in kind of a hurry and forgot about the code thing, I edited my post and won't forget again.
|
Try moving the "num" declaration to the top of the code, before you define the other functions that reference it.
|
| All times are GMT -5. The time now is 4:38 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC