Python Forum
First Post/ Class Error Question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: First Post/ Class Error Question (/thread-3134.html)



First Post/ Class Error Question - jvan1601 - Apr-30-2017

Hello everyone, this is my first post on this (or any) forum. Python is my first programming language, and I am learning it on my own time as a hobby. I am using a couple of different books (Python Crash Course and Learn Python the Hard Way) to get started and working through the exercises as they come up. I am currently working with modules and classes. I keep getting a NameError that says "Self is not defined" and I'm not quite sure I understand why. My question is I'm not exactly sure what my error is and how to correct it (?) 

class Dog(object):
"""A simple attempt to model a dog."""
def __init__(self, name, age):
"""Initialize name and age attributes."""
self.name = name
self.age = age
def sit(self):
"""Simulate a dog sitting in response to a command."""
print(self.name.title() + " is now sitting.")
def roll_over(self): """Simulate rolling over in response to acommand."""
print(self.name.title() + " rolled over!")
my_dog = Dog('willie', 6)
print("My dog's name is " + my_dog.name.title() + ".")
print("My dog is " + str(my_dog.age) + " years old.")  
This produces: 

Error:
Traceback (most recent call last):   File "dog.py", line 1, in <module>     class Dog(object):   File "dog.py", line 9, in Dog     print(self.name.title() + " is now sitting.") NameError: name 'self' is not defined
P.S. I apologize if I didn't word my question or problem correctly, or if there is a thread identical to this. I'm not totally sure how to begin asking something like this.  Thanks for any response!


RE: First Post/ Class Error Question - buran - Apr-30-2017

Please, repost your code with proper indentation. Most probably that's the problem, but the code in your post does not produce this error
class Dog(object):
   """A simple attempt to model a dog."""
   def __init__(self, name, age):
       """Initialize name and age attributes."""
       self.name = name
       self.age = age
   def sit(self):
       """Simulate a dog sitting in response to a command."""
       print(self.name.title() + " is now sitting.")
   def roll_over(self):
       """Simulate rolling over in response to acommand."""
       print(self.name.title() + " rolled over!")
my_dog = Dog('willie', 6)
print("My dog's name is " + my_dog.name.title() + ".")
print("My dog is " + str(my_dog.age) + " years old.")
Output:
My dog's name is Willie. My dog is 6 years old.



RE: First Post/ Class Error Question - jvan1601 - May-01-2017

I guess it was an indentation error. I appreciate your help, after running and copying your indentations it did work. In my file I did have most of the indentations correct, I guess I just missed one.

Thanks again!