Solved

Posted on 2007-08-07

im new to python - i only started learning about it yesterday as i want o learn a new language.

Ive being following some tutorials to get me started. im working with classes and cannot fix this issue:

The shell window returns this:

Traceback (most recent call last):

File "W:/Python/examples/ex14.py", line 2, in <module>

class Shape:

File "W:/Python/examples/ex14.py", line 18, in Shape

self.y = self.y * scale

NameError: name 'self' is not defined

Here is my code:

#An example of a class

class Shape:

def __init__(self,x,y):

self.x = x

self.y = y

description = "This shape has not been described yet"

author = "Nobody has claimed to make this shape yet"

def area(self):

return self.x * self.y

def perimeter(self):

return 2 * self.x + 2 * self.y

def describe(self,text):

self.description = text

def authorName(self,text):

self.author = text

def scaleSize(self,scale):

self.x = self.x * scale

self.y = self.y * scale

rectangle = Shape(100,45)

#finding the area of your rectangle:

print rectangle.area()

#finding the perimeter of your rectangle:

print rectangle.perimeter()

#describing the rectangle

rectangle.describe("A wide rectangle, more than twice\

as wide as it is tall")

#making the rectangle 50% smaller

rectangle.scaleSize(0.5)

#re-printing the new area of the rectangle

print rectangle.area()

Ive being following some tutorials to get me started. im working with classes and cannot fix this issue:

The shell window returns this:

Traceback (most recent call last):

File "W:/Python/examples/ex14.p

class Shape:

File "W:/Python/examples/ex14.p

self.y = self.y * scale

NameError: name 'self' is not defined

Here is my code:

#An example of a class

class Shape:

def __init__(self,x,y):

self.x = x

self.y = y

description = "This shape has not been described yet"

author = "Nobody has claimed to make this shape yet"

def area(self):

return self.x * self.y

def perimeter(self):

return 2 * self.x + 2 * self.y

def describe(self,text):

self.description = text

def authorName(self,text):

self.author = text

def scaleSize(self,scale):

self.x = self.x * scale

self.y = self.y * scale

rectangle = Shape(100,45)

#finding the area of your rectangle:

print rectangle.area()

#finding the perimeter of your rectangle:

print rectangle.perimeter()

#describing the rectangle

rectangle.describe("A wide rectangle, more than twice\

as wide as it is tall")

#making the rectangle 50% smaller

rectangle.scaleSize(0.5)

#re-printing the new area of the rectangle

print rectangle.area()

8 Comments

my script works perfect now..

thank you.

Traceback (most recent call last):

File "W:/Python/examples/ex14.p

class Shape:

File "W:/Python/examples/ex14.p

self.y = self.y * scale

NameError: name 'self' is not defined

So, it was easy to spot the problem when you know that it is on the line 18. You, as a Python beginner, were only slighly blinded by not knowing what should you search for ;)

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

Analyzing stocks to get a specific profile | 2 | 124 | |

Permutations of numbers in columns and lines | 1 | 132 | |

How to structure a small domotics database | 4 | 59 | |

Pthon mysql - how to count number of rows in a table | 3 | 31 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**20** Experts available now in Live!