Why am I getting a NameError when I try to access an attribute in my class?

Ennv

I have this code with a class:

class Triangle(object):
    def __init__(self, side1, side2, side3):
        self.side1 = side1
        self.side2 = side2
        self.side3 = side3

    def perimeter(self):
        return "Perimeter = %s" % (side1 + side2 + side3)

a = Triangle(3, 4, 5)
print(a.perimeter())

Running this code throws an exception:

Traceback (most recent call last):
  File "untitled.py", line 12, in <module>
    print(a.perimeter())
  File "untitled.py", line 9, in perimeter
    return "Perimeter = %s" % (side1 + side2 + side3)
NameError: name 'side1' is not defined

How come I can't access side1 in the perimeter method?

Rakesh

Replace

return "Perimeter = %s" %(side1 + side2 + side3)

To

return "Perimeter = %s" %(self.side1 + self.side2 + self.side3)

You missed to add self

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why am I getting a NameError when I try to call my function?

Why am I getting NameError?

Why am I getting these error messages when I try to view media files on my Android device?

Why am I getting these warnings when I try to installing Pycharm in my Pop!_OS?

Why I am getting 'Forbidden' when I try to login on my live codeigniter site?

Why am i getting this error when i try to answer my own dns request?

Why am I getting this TypeError when I try to slice my Pandas DataFrame?

Why am I getting a Segmentation fault when I try to add a new node to my linked list?

Why am I getting an error when I try to get a string from my table in a Jframe?

Why am I getting an 'undefined' when I try to call my function?

Why am I getting a '400: Bad Request' when i try to change my Discord avatar?

Why am I getting an error message when I try to deploy my Gatsby website?

Why am i getting a nullpointerexception when I try to setAdapter?

Why am I getting NULL when I try to initiate a connection?

Why am I getting "NameError: name 'qiskit' is not defined" on my program when I already imported all qiskit functions

Why am I getting NameError: 'Wolf' is not defined

Why am i getting undefined in my console, when i try to subscribe and use my context using typescript and react

Why am I getting NaN when executing a method within my class, but not when executing in isolation?

Why am I getting an error when I try to print the contents of a file I am searching for?

When I try to deploy my PHP application to Google App Engine, why am I suddenly getting a 400 error?

Why am I getting a cannot read property message in my console when I try to use children property and offsetWidth?

Why am I getting an error when I try to use ngrok to create a http tunnel for my local host 5000 for Twilio?

How to access custom exception class members when using catch(e)? And why exactly am I getting this error?

Why am I getting an ActiveRecord::StatementInvalid error when using the `joins` method on my model class?

Why am i getting an empty value from my doInBackground when using AsyncTask in an IntentService class?

Why am I getting TypeError at runtime with my generic StatefulWidget class?

Why am I getting this compilation error in my abstract base class?

Why am I getting a capture-of applicability error in my class?

Why am i getting NoSuchElementException error in my class