Python Forum
First Post/ Class Error Question
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
First Post/ Class Error Question
#1
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!
Reply
#2
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.
Reply
#3
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  newbie question....importing a created class ridgerunnersjw 5 1,013 Oct-01-2020, 07:59 PM
Last Post: ridgerunnersjw
  error in class non_name092 1 859 Sep-02-2020, 05:42 PM
Last Post: bowlofred
  Class question robdineen 7 1,325 May-30-2020, 05:44 AM
Last Post: Calli
  Question about naming variables in class methods sShadowSerpent 1 895 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  class question jrauden 1 706 Feb-16-2020, 10:14 AM
Last Post: ibreeden
  My class has name error message 357mag 3 1,155 Sep-04-2019, 03:29 PM
Last Post: snippsat
  Class Question esteel 5 2,532 May-22-2018, 10:27 AM
Last Post: esteel
  No route to host error when using requests.post in python on raspberry pi mariummalik22 0 2,829 Jan-06-2018, 08:34 PM
Last Post: mariummalik22
  Class Error OmarSinno 1 2,038 Sep-28-2017, 05:49 AM
Last Post: Mekire
  Error Type: <class 'OSError'> And Error Type: <class 'ValueError'> runnerpaul 1 2,495 Jul-18-2017, 03:08 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020