The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Variable not defined?!?
Discuss Variable not defined?!? in the Python Programming forum on Dev Shed. Variable not defined?!? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 29th, 2003, 06:34 PM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Posts: 67
Time spent in forums: 41 m
Reputation Power: 10
|
|
Variable not defined?!?
When I run this program:
Code:
# Dice Roller Program
# By Evan Patterson
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
# Labels
self.label1 = Label(master, text="# of Rolls:")
self.label2 = Label(master, text="# of Sides:")
self.label3 = Label(master, text="Results:")
self.label4 = Label(master, text="Dice Roller\n by Evan Patterson")
self.label1.grid(sticky=E)
self.label2.grid(sticky=E)
self.label3.grid(sticky=E)
self.label4.grid(row=0, column=2, columnspan=2, rowspan=2)
# Entries
self.entry1 = Entry(master)
self.entry2 = Entry(master)
self.entry1.grid(row=0, column=1)
self.entry2.grid(row=1, column=1)
# Buttons
self.exitb = Button(master, text="Exit", command=root.destroy)
self.rollb = Button(master, text="Roll!", command=self.roll)
self.resetb = Button(master, text="Clear Results", command=self.clear)
self.resetb.grid(row=3, column=2)
self.exitb.grid(row=3, column=3)
self.rollb.grid(row=2, columnspan=2, column=2)
# Checkpoint
rollopt = IntVar()
dontaddrolls = Checkbutton(root, text="Don't Add Rolls Together", variable=rollopt, onvalue="1", offvalue="0")
dontaddrolls.grid(columnspan=2, column=0, row=3, sticky=W)
# Text Box
self.text = Text(master, height=4, width=20,)
self.text.grid(row=2, column=1)
self.text.config(state=DISABLED)
def clear(self):
self.text.config(state=NORMAL)
self.text.delete(1.0, END)
self.text.config(state=DISABLED)
def roll(self):
x = rollopt.get()
if x == 0:
while self.entry1 > 0:
rollr = random.randint(1, self.entry2)
rollrsum = rollrsum + rollr
self.entry1 = self.entry1 - 1
self.text.insert(END, rollrsum)
#else:
root = Tk()
app = App(root)
root.title("Dice Roller")
root.mainloop()
It gives me this error:
Code:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python23\lib\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args)
File "C:\Python23\My Programs\Dice Roller.txt", line 52, in roll
x = rollopt.get()
NameError: global name 'rollopt' is not defined
I tried everything but it still happens. Do you know what's wrong and how it can be fixed?
Thanks for help.
__________________
Before you criticize someone, walk a mile in their shoes, that way when you do criticize them, you're a mile away and you have their shoes!
|

August 29th, 2003, 07:01 PM
|
 |
Wacky hack
|
|
Join Date: Apr 2001
Location: London, England
Posts: 513
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
|
|
|
When you define "rollopt" in App.__init__ you are assigning it the namespace of that init method, not of the whole class. You need to assign it as "self.rollopt" and then reference it as such within the class...
< rollopt = IntVar()
---
> self.rollopt = IntVar()
< x = rollopt.get()
---
> x = self.rollopt.get()
|

August 29th, 2003, 07:01 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Balor,
'rollopt' isn't visable by your roll method  so you wont be able to use it, inorder to make it available you can either pass the value manually i.e roll(rollopt).. or change rollopt to self.rollopt  , personally i'd go for the second
This should work, if not let me know
Mark
|

August 29th, 2003, 07:04 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
one, two, three jinx telex  , i guess great minds do think alike lol
Mark.
|

August 29th, 2003, 07:21 PM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Posts: 67
Time spent in forums: 41 m
Reputation Power: 10
|
|
Your first suggestion gave me this error:
Code:
Traceback (most recent call last):
File "C:\Python23\My Programs\Dice Roller.txt", line 62, in -toplevel-
app = App(root)
File "C:\Python23\My Programs\Dice Roller.txt", line 38, in __init__
dontaddrolls = Checkbutton(root, text="Don't Add Rolls Together", variable=rollopt, onvalue="1", offvalue="0")
NameError: global name 'rollopt' is not defined
Your second gave me this:
Code:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python23\lib\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args)
TypeError: roll() takes exactly 2 arguments (1 given)
This doesn't make sense...
|

August 29th, 2003, 07:24 PM
|
 |
Wacky hack
|
|
Join Date: Apr 2001
Location: London, England
Posts: 513
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
|
|
Heh
There are also some other problems with this code...
In the "roll" function, the line "if x == 0:" won't ever return true because x is an object, not a variable, so even if there are no values in rollopt, it will still always exist.
You also refer to "self.entry1" rather than "self.entry1.get()" when getting the information from the Entry forms. You'll then need to perform an int() operation on those to turn the strings into numbers.
Here's a little amended code:
Code:
def roll(self):
x = self.rollopt.get()
#if x == 0:
if 1 is 1: # always true, just to retain indentation
rollrsum = 0
while int(self.entry1.get()) > 0:
# Deduct one from rolls to go
newval = int(self.entry1.get())
newval = newval - 1
self.entry1.delete(0, END)
self.entry1.insert(END, newval)
# Add new value to list
rollr = random.randint(1, int(self.entry2.get()))
rollrtext = "".join([str(rollr), " + ", str(rollrsum), " = ", str(rollr + rollrsum), "\n"])
rollrsum = rollrsum + rollr
self.text.config(state=NORMAL)
self.text.insert(END, rollrtext)
self.text.config(state=DISABLED)
|

August 29th, 2003, 07:26 PM
|
 |
Wacky hack
|
|
Join Date: Apr 2001
Location: London, England
Posts: 513
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
|
|
Quote: Originally posted by Baltor
Your first suggestion gave me this error:
Code:
Traceback (most recent call last):
File "C:\Python23\My Programs\Dice Roller.txt", line 62, in -toplevel-
app = App(root)
File "C:\Python23\My Programs\Dice Roller.txt", line 38, in __init__
dontaddrolls = Checkbutton(root, text="Don't Add Rolls Together", variable=rollopt, onvalue="1", offvalue="0")
NameError: global name 'rollopt' is not defined
|
You need to change "variable=rollopt" to "variable=self.rollopt"...
You really need to make sure your objects are in the correct namespace every time you use them.
|

August 29th, 2003, 07:43 PM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Posts: 67
Time spent in forums: 41 m
Reputation Power: 10
|
|
Now, something is wrong with the random part. Here is the error:
Code:
Traceback (most recent call last):
File "C:\Python23\lib\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args)
File "C:\Python23\My Programs\diceroller.py", line 63, in roll
rollrtext = "".join([str(rollr), " + ", str(rollrsum), " = ", str(rollr + rollrsum), "\n"])
NameError: global name 'random' is not defined
I remember having this problem before...
Thanks.
|

August 29th, 2003, 07:49 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
Baltor, to use the randint() function from random you need to import the random module..
Mark
|

August 29th, 2003, 08:48 PM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Posts: 67
Time spent in forums: 41 m
Reputation Power: 10
|
|
|
Thanks for be so patient with me, I am new to Python and programming in general.
|

August 30th, 2003, 02:41 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
That's ok Baltar, we all had to start somewhere 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|