![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2005
Posts: 3
Rep Power: 0
![]() |
Weird NameError
Okay, I will be quick and to the point. The below code returns a NameError: global name 'troid' is not defined.
Can someone tell me what I did wrong? #Metroid Pet Sim
#Written by Alex Johnson
#If someone tells you otherwise shove him into a 'Troid nuts-first.
def main():
print "Metroid pet-sim in development!"
troid = raw_input("Name your new baby Metroid! ")
prompt()
def prompt():
print troid
main() |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5
![]() |
your problem is scope ... in python variables created (first use) in a function only have scope in that function and therefore do not exist once the function has completed. try moving your variable outside of the function or returning it from main() and passing it to prompt()
check http://www.python.org/doc for scope rules.
__________________
[ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ]; /* Don't make me use it! */ |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
You need to define your function before you call it. Also make troid global so the other function can use it.
#If someone tells you otherwise shove him into a 'Troid nuts-first.
def prompt():
print troid
def main():
global troid
print "Metroid pet-sim in development!"
troid = raw_input("Name your new baby Metroid! ")
prompt()
main()
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
You don't need to define your function before you use it.
>>> def foo(): . bar() . >>> def bar(): . print "Yeah.." . >>> foo() Yeah. def prompt(s):
print s
def main():
print "Metroid ..."
troid = raw_input("Name your ...")
prompt(troid) |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jul 2005
Posts: 3
Rep Power: 0
![]() |
Okay, thanks. Because of the way I'm used to programming I'm just going to be making all variables global, now that I know you can (and need) to do that. I simply assumed they were always global.
__________________
def repeat():
lather()
rinse()
repeat()And thus, the story of the programmer who died of old age while washing his hair. |
|
|
|
|
|
#6 | |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() ![]() ![]() |
I agree, regardless of the language... keep the use of global variables down as much as possible. There is a reason for variable scoping
![]()
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
[quote=Cerulean]You don't need to define your function before you use it.
>>> def foo(): . bar() . >>> def bar(): . print "Yeah.." . >>> foo() Yeah. The silly command line scribbles prove nothing. Try this # define a function before it is called or get an error
print doubleValue(5) # gives NameError: name 'doubleValue' is not defined
def doubleValue(n):
n = n * 2
return n
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#9 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Dietrich is right. You do need to declare and define a function before you use it, but not before you tell the computer it may be used in the future.
|
|
|
|
|
|
#10 | |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
![]() |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|