NameErroring: lost in space

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 3181 Views - Last Post: 24 July 2012 - 07:33 AM Rate Topic: -----

#1 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

NameErroring: lost in space

Posted 23 July 2012 - 01:05 AM

ok, so I was making a class for collecting data on basic astrophysics characteristics like mass and radius, etc. set up like so.
class thing():
   def thisthing():

   def thatthing():

   def printthing():

etc.


I was in the process of creating method that utilizes a "table" of possible values that they could fall into where such data would be gathered through input and the it outputs the star type in another method. When I set up the "asking" method, I gathered the data necessary and set it to display the star type method. The result? I consistently get a NameError like so: "NameError: global name 'star_type' is not defined". I'm not sure exactly why its behaving this way, maybe you guys will know a bit more.

pasting relevant code:
def constant():
        global sun_mass
        sun_mass = 1.9891*(10**30) #kilograms: kg
   
        global sun_radi
        sun_radi = 6.96242*(10**8) #meters: m

def star_type(mass,radius):
        classification = ["O","B","A","F","G","K","M"]
        if mass <= (0.45*sun_mass) and radius <= (0.70*sun_radi):
            return classification[6]
        elif (0.45*sun_mass) < mass < (0.80*sun_mass) and (0.70*sun_radi) < radius < (0.96*sun_radi):
            return classification[5]
        elif (0.80*sun_mass) < mass < (1.04*sun_mass) and (0.96*sun_radi) < radius < (1.15*sun_radi):
            return classification[4]
        elif (1.04*sun_mass) < mass < (1.40*sun_mass) and (1.15*sun_radi) < radius < (1.40*sun_radi):
            return classification[3]
        elif (1.40*sun_mass) < mass < (2.10*sun_mass) and (1.40*sun_radi) < radius < (1.80*sun_radi):
            return classification[2]
        elif (2.10*sun_mass) < mass < (16.0*sun_mass) and (1.80*sun_radi) < radius < (6.60*sun_radi):
            return classification[1]
        elif mass >= (16.0*sun_mass) and radius >= (6.60*sun_radi):
            return classification[0]
        else:
            return "No such type exists/This star is extremely unusual"

def ask():
        star_mass = 1.98*(10**30) #eval(raw_input("Mass of Star: "))
        star_radi = 6.78*(10**8) #eval(raw_input("Radius of Star: "))
        print star_type(star_mass,star_radi)
ask()



The output should be a "G" type star but it nameErrors, what do?

[edit:] Also note, all my other global constant values have no problem in being defined with some of the other functions/methods, but it's being a bit snotty with sun_mass/sun_radi with some of the varying attempts I've made since posting.

Is This A Good Question/Topic? 1
  • +

Replies To: NameErroring: lost in space

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: NameErroring: lost in space

Posted 23 July 2012 - 01:31 AM

You never call your constant function, so sun_mass and sun_radi are never set. If you call constant before calling star_type, the code will work fine.

PS: In the future please copy and paste the exact error message (including the line number), instead of reproducing it from your memory. I've spent way too much time wondering how star_type could not be defined in your code, before I realized that you simply messed up the error message.
Was This Post Helpful? 0
  • +
  • -

#3 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: NameErroring: lost in space

Posted 23 July 2012 - 10:30 AM

View Postsepp2k, on 23 July 2012 - 03:31 AM, said:

You never call your constant function, so sun_mass and sun_radi are never set. If you call constant before calling star_type, the code will work fine.

PS: In the future please copy and paste the exact error message (including the line number), instead of reproducing it from your memory. I've spent way too much time wondering how star_type could not be defined in your code, before I realized that you simply messed up the error message.

def constant():
        global sun_mass
        sun_mass = 1.9891*(10**30)
        global sun_radi
        sun_radi = 6.96242*(10**8)

def star_type(mass,radius):
        sun_mass = 1.9891*(10**30)
        sun_radi = 6.96242*(10**8)
        classification = ["O","B","A","F","G","K","M"]

        stuff...

print star_type(4.0,5.0)


Hmm, it does check out fine when I call those constants within the star_type method, however the repetition of that code is ugly to me. Any way around having to be redundant as I have no problem with calling any of my other constants in my other expressions. Also, provided I leave the redundant code...

If this is my code:
def ask():
        print star_type(5.0,4.0)
ask()


it still produces the same error for anything being defined inside the method (including any other methods as well); however, it does not encounter said problem when expressed outside of the method but only before the compiler reads the that method [ask():].
Traceback (most recent call last):
  File "C:\astronomy.py", line 2, in <module>
    class astrophysics():
  File "C:astronomy.py", line 129, in astrophysics
    ask()
  File "C:\astronomy.py", line 125, in ask
    print star_type(5.0,4.0)
NameError: global name 'star_type' is not defined


the below "works":
print star_type(5.0,4.0)
def ask():
        star_mass = 1.23456
        star_radi = 6.54321
ask()


with output like so:
>>> M


but the thing is, that's nothing more than occupying the whitespace between the two methods. And I'm still confused to hell why it fails to call my method in the above sections of code.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: NameErroring: lost in space

Posted 23 July 2012 - 10:35 AM

View Postsepp2k, on 23 July 2012 - 10:31 AM, said:

You never call your constant function, so sun_mass and sun_radi are never set. If you call constant before calling star_type, the code will work fine.

Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: NameErroring: lost in space

Posted 23 July 2012 - 10:40 AM

This is just asinine. Remove the constant function because it's pointless and your code will work:
sun_mass = 1.9891*(10**30) #kilograms: kg
sun_radi = 6.96242*(10**8) #meters: m

def star_type(mass,radius):
        classification = ["O","B","A","F","G","K","M"]
        if mass <= (0.45*sun_mass) and radius <= (0.70*sun_radi):
            return classification[6]
        elif (0.45*sun_mass) < mass < (0.80*sun_mass) and (0.70*sun_radi) < radius < (0.96*sun_radi):
            return classification[5]
        elif (0.80*sun_mass) < mass < (1.04*sun_mass) and (0.96*sun_radi) < radius < (1.15*sun_radi):
            return classification[4]
        elif (1.04*sun_mass) < mass < (1.40*sun_mass) and (1.15*sun_radi) < radius < (1.40*sun_radi):
            return classification[3]
        elif (1.40*sun_mass) < mass < (2.10*sun_mass) and (1.40*sun_radi) < radius < (1.80*sun_radi):
            return classification[2]
        elif (2.10*sun_mass) < mass < (16.0*sun_mass) and (1.80*sun_radi) < radius < (6.60*sun_radi):
            return classification[1]
        elif mass >= (16.0*sun_mass) and radius >= (6.60*sun_radi):
            return classification[0]
        else:
            return "No such type exists/This star is extremely unusual"

def ask():
        star_mass = 1.98*(10**30) #eval(raw_input("Mass of Star: "))
        star_radi = 6.78*(10**8) #eval(raw_input("Radius of Star: "))
        print star_type(star_mass,star_radi)
ask()



This post has been edited by atraub: 23 July 2012 - 10:43 AM

Was This Post Helpful? 1
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: NameErroring: lost in space

Posted 23 July 2012 - 10:55 AM

Actually, if you want to do this right, you won't use global variables to begin with...

def star_type(mass,radius):
        classification = ["O","B","A","F","G","K","M"]
        sun_mass = 1.9891*(10**30) #kilograms: kg
        sun_radi = 6.96242*(10**8) #meters: m

        if mass <= (0.45*sun_mass) and radius <= (0.70*sun_radi):
            return classification[6]
        elif (0.45*sun_mass) < mass < (0.80*sun_mass) and (0.70*sun_radi) < radius < (0.96*sun_radi):
            return classification[5]
        elif (0.80*sun_mass) < mass < (1.04*sun_mass) and (0.96*sun_radi) < radius < (1.15*sun_radi):
            return classification[4]
        elif (1.04*sun_mass) < mass < (1.40*sun_mass) and (1.15*sun_radi) < radius < (1.40*sun_radi):
            return classification[3]
        elif (1.40*sun_mass) < mass < (2.10*sun_mass) and (1.40*sun_radi) < radius < (1.80*sun_radi):
            return classification[2]
        elif (2.10*sun_mass) < mass < (16.0*sun_mass) and (1.80*sun_radi) < radius < (6.60*sun_radi):
            return classification[1]
        elif mass >= (16.0*sun_mass) and radius >= (6.60*sun_radi):
            return classification[0]
        else:
            return "No such type exists/This star is extremely unusual"



This post has been edited by atraub: 23 July 2012 - 10:56 AM

Was This Post Helpful? 0
  • +
  • -

#7 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: NameErroring: lost in space

Posted 23 July 2012 - 10:57 AM

View Postatraub, on 23 July 2012 - 12:40 PM, said:

This is just asinine. Remove the constant function because it's pointless and your code will work:

I'd agree with you if it wasn't for the fact on two parts; 1.), I will require to call that function later down the road in other classes, projects, or otherwise and 2.), the snippet of code you provided (of which I tried similar versions, [edit:] including the version you posted but I just missed) exists within a class rather than the standalone method that it is. If I just ran your segment as-is with a variation, much anguish occurs...
class equation():
    sun_mass = 1.9891*(10**30) #kilograms: kg
    sun_radi = 6.96242*(10**8) #meters: m

    def star_type(mass,radius):
            classification = ["O","B","A","F","G","K","M"]
    
            all that stuff...            

    def ask():
            star_mass = 1.98*(10**30) #eval(raw_input("Mass of Star: "))
            star_radi = 6.78*(10**8) #eval(raw_input("Radius of Star: "))
            print star_type(star_mass,star_radi)
    ask()


...with this wonderful error message.
Traceback (most recent call last):
  File "C:/test_sample.py", line 1, in <module>
    class equation():
  File "C:/test_sample.py", line 28, in equation
    ask()
  File "C:/test_sample.py", line 27, in ask
    print star_type(star_mass,star_radi)
NameError: global name 'star_type' is not defined


This post has been edited by BloodyInari: 23 July 2012 - 11:01 AM

Was This Post Helpful? 0
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: NameErroring: lost in space

Posted 23 July 2012 - 11:31 AM

You really shouldn't be condescending to the people who are trying to help you, particularly when their skills are so far beyond yours. That error is due to your lack of understanding of how classes work.

EDIT:
Here's what you were trying to do...
class Equation():
    def star_type(self, mass,radius):
            classification = ["O","B","A","F","G","K","M"]
            sun_mass = 1.9891*(10**30) #kilograms: kg
            sun_radi = 6.96242*(10**8) #meters: m

            if mass <= (0.45*sun_mass) and radius <= (0.70*sun_radi):
                return classification[6]
            elif (0.45*sun_mass) < mass < (0.80*sun_mass) and (0.70*sun_radi) < radius < (0.96*sun_radi):
                return classification[5]
            elif (0.80*sun_mass) < mass < (1.04*sun_mass) and (0.96*sun_radi) < radius < (1.15*sun_radi):
                return classification[4]
            elif (1.04*sun_mass) < mass < (1.40*sun_mass) and (1.15*sun_radi) < radius < (1.40*sun_radi):
                return classification[3]
            elif (1.40*sun_mass) < mass < (2.10*sun_mass) and (1.40*sun_radi) < radius < (1.80*sun_radi):
                return classification[2]
            elif (2.10*sun_mass) < mass < (16.0*sun_mass) and (1.80*sun_radi) < radius < (6.60*sun_radi):
                return classification[1]
            elif mass >= (16.0*sun_mass) and radius >= (6.60*sun_radi):
                return classification[0]
            else:
                return "No such type exists/This star is extremely unusual"


    def ask(self):
            star_mass = 1.98*(10**30) #eval(raw_input("Mass of Star: "))
            star_radi = 6.78*(10**8) #eval(raw_input("Radius of Star: "))
            return self.star_type(star_mass,star_radi)



Now to test it in the shell...
>>> x = Equation()
>>> x.ask()
'G'
>>> 


That being said, turning this tiny bit of code into a class is pointless. If there's more, that would make sense.

EDIT:
If there's a second equation that uses sun_mass and sun_radi then it makes more sense for those values to be more globalish, then we'd do something like this:
class Equation():
    def __init__(self):
            self.sun_mass = 1.9891*(10**30) #kilograms: kg
            self.sun_radi = 6.96242*(10**8) #meters: m
    
    def star_type(self, mass,radius):
            classification = ["O","B","A","F","G","K","M"]

            if mass <= (0.45*self.sun_mass) and radius <= (0.70*self.sun_radi):
                return classification[6]
            elif (0.45*self.sun_mass) < mass < (0.80*self.sun_mass) and (0.70*self.sun_radi) < radius < (0.96*self.sun_radi):
                return classification[5]
            elif (0.80*self.sun_mass) < mass < (1.04*self.sun_mass) and (0.96*self.sun_radi) < radius < (1.15*self.sun_radi):
                return classification[4]
            elif (1.04*self.sun_mass) < mass < (1.40*self.sun_mass) and (1.15*self.sun_radi) < radius < (1.40*self.sun_radi):
                return classification[3]
            elif (1.40*self.sun_mass) < mass < (2.10*self.sun_mass) and (1.40*self.sun_radi) < radius < (1.80*self.sun_radi):
                return classification[2]
            elif (2.10*self.sun_mass) < mass < (16.0*self.sun_mass) and (1.80*self.sun_radi) < radius < (6.60*self.sun_radi):
                return classification[1]
            elif mass >= (16.0*self.sun_mass) and radius >= (6.60*self.sun_radi):
                return classification[0]
            else:
                return "No such type exists/This star is extremely unusual"


    def ask(self):
            star_mass = 1.98*(10**30) #eval(raw_input("Mass of Star: "))
            star_radi = 6.78*(10**8) #eval(raw_input("Radius of Star: "))
            return self.star_type(star_mass,star_radi)



My code doesn't generate wonderful error messages.

This post has been edited by atraub: 23 July 2012 - 12:09 PM

Was This Post Helpful? 1
  • +
  • -

#9 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: NameErroring: lost in space

Posted 23 July 2012 - 11:39 AM

View Postatraub, on 23 July 2012 - 01:31 PM, said:

You really shouldn't be condescending to the people who are trying to help you, particularly when their skills are so far beyond yours. That error is due to your lack of understanding of how classes work.

I didnt mean to imply condescension, I apologize if it came across as such, in fact I was trying to be a bit humorous... :/

And I should note, this is all self-taught attempts at learning class structures, I have no more idea how this works than what PDFs I've read and the reflection of such. I'm just doing the best with what grasp I have by trying to tinker and manipulate the code into some form of understanding. Stabs in the dark, more or less.

This post has been edited by BloodyInari: 23 July 2012 - 11:45 AM

Was This Post Helpful? 0
  • +
  • -

#10 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: NameErroring: lost in space

Posted 23 July 2012 - 11:50 AM

Since your newer to classes, I'll explain the self keyword a little.

Classes have an attribute called self. Every method within a class must have self as its first parameter. If you want to have a variables that are available to all the methods within a class, you define those variables in the __init__ method, with the syntax self.variableName. You can then access those variables within the methods of your class with the same syntax.

EDIT:
Also, when one method in your class calls another method of the same class, you must use self.methodName. An example of this can be seen in the supplied "ask" method.

EDIT 2:
Also, sorry I misinterpreted what you said.

This post has been edited by atraub: 23 July 2012 - 01:49 PM

Was This Post Helpful? 1
  • +
  • -

#11 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: NameErroring: lost in space

Posted 23 July 2012 - 12:08 PM

Ok, that makes a lot more sense and explains what I was missing. Thanks, I'll mess around with what you posted and see about whipping that code into shape :notify:
Was This Post Helpful? 0
  • +
  • -

#12 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: NameErroring: lost in space

Posted 23 July 2012 - 01:11 PM

Awesome! I got a working prototype for now and can continue onwards with my project. Thanks for the help atraub!
Was This Post Helpful? 0
  • +
  • -

#13 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: NameErroring: lost in space

Posted 23 July 2012 - 10:15 PM

View Postatraub, on 23 July 2012 - 08:50 PM, said:

Since your newer to classes, I'll explain the self keyword a little.


self isn't a keyword.

Quote

Classes have an attribute called self. Every method within a class must have self as its first parameter.


Every (instance) method must have at least one parameter and the value of the first parameter will be the object that method is called on (so if you do foo.bar(baz) the first argument to bar will be foo and the second will be baz). The name of the first argument can be whatever you want, but by convention people choose to call it self.

This post has been edited by sepp2k: 23 July 2012 - 10:16 PM

Was This Post Helpful? 0
  • +
  • -

#14 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: NameErroring: lost in space

Posted 24 July 2012 - 07:24 AM

View Postsepp2k, on 24 July 2012 - 01:15 AM, said:

View Postatraub, on 23 July 2012 - 08:50 PM, said:

Since your newer to classes, I'll explain the self keyword a little.


self isn't a keyword.

Quote

Classes have an attribute called self. Every method within a class must have self as its first parameter.


Every (instance) method must have at least one parameter and the value of the first parameter will be the object that method is called on (so if you do foo.bar(baz) the first argument to bar will be foo and the second will be baz). The name of the first argument can be whatever you want, but by convention people choose to call it self.


A bit nitpicky, are we? Yes, technically calling self a keyword was wrong. That being said, calling it anything other than self would be retarded.

This post has been edited by atraub: 24 July 2012 - 07:29 AM

Was This Post Helpful? 0
  • +
  • -

#15 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: NameErroring: lost in space

Posted 24 July 2012 - 07:28 AM

I prefer the term "precise". Calling self a keyword and making it sound as if the first argument to a method had to be called self gives people the wrong idea. It hides the elegance of Python's object model and makes it sound as if there were magic going on where there is none.

When you do my_object.my_method(args) where my_object is an object of class my_class calls my_class.my_method(my_object, args). So my_method is called with my_object as a regular argument. That's a perfectly simple model. Talking about a self keyword makes things look more complicated than they really are.

This post has been edited by sepp2k: 24 July 2012 - 07:31 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2