Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 19th, 2005, 1:30 AM   #1
Minion
Newbie
 
Minion's Avatar
 
Join Date: Jul 2005
Posts: 3
Rep Power: 0 Minion is on a distinguished road
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()
Minion is offline   Reply With Quote
Old Jul 19th, 2005, 4:58 AM   #2
sykkn
Hobbyist Programmer
 
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5 sykkn is on a distinguished road
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! */
sykkn is offline   Reply With Quote
Old Jul 19th, 2005, 6:59 AM   #3
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

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!
Dietrich is offline   Reply With Quote
Old Jul 19th, 2005, 9:19 AM   #4
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
You don't need to define your function before you use it.
>>> def foo():   
  .     bar()
  .     
>>> def bar():
  .     print "Yeah.."
  .     
>>> foo()
Yeah.
You do, however, need to make troid global in this example, but the better solution is to pass the troid variable as a parameter to the prompt function, like so:
def prompt(s):
    print s

def main():
    print "Metroid ..."
    troid = raw_input("Name your ...")
    prompt(troid)
Cerulean is offline   Reply With Quote
Old Jul 19th, 2005, 11:14 PM   #5
Minion
Newbie
 
Minion's Avatar
 
Join Date: Jul 2005
Posts: 3
Rep Power: 0 Minion is on a distinguished road
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.
Minion is offline   Reply With Quote
Old Jul 20th, 2005, 7:41 AM   #6
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
Because of the way I'm used to programming I'm just going to be making all variables global
Eeek. Global variables are great when used properly, but just using them as a general rule in any programming language is bad (explaned by Wikipedia). It's bad in Python specifically because accessing global variables is a slower and more costly than accessing local variables.
Cerulean is offline   Reply With Quote
Old Jul 20th, 2005, 8:08 AM   #7
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Jul 20th, 2005, 8:20 AM   #8
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

[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!
Dietrich is offline   Reply With Quote
Old Jul 20th, 2005, 3:34 PM   #9
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jul 20th, 2005, 3:52 PM   #10
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
Originally Posted by Dietrich
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
By "use it", I did not mean call it. I meant reference it in another function. I'm afraid your code example is the flawed one. Please look at his example, which uses code like the code used in my "command line scribble". As you see, he calls the main function at the bottom of the file, after the definition of main and troid. In his example, his error had nothing to do with where the prompt function was placed.
Cerulean is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC