AttributeError: 'int' object has no attribute 'radius'

How do i fix this?
This is my codes:
circle.py:

class Circle():
    def __init__(self,radius):
        self.radius = radius

def circ_area(self):
    return self.radius**2*3.14

def circ_circum(self):
    return 2*self.radius*3.14

rect.py

class Rect():
    def __init__(self, l,w):
        self.length = l
        self.width = w

def rect_area(self):
    return self.radius*self.width

def rect_perim(self):
    return 2*(self.radius + self.width)

geometry.py:

import circle, rect

radius = int(input("Enter the radius of the circle: "))
length = int(input("Enter the length of rectangle: "))
width = int(input("Enter the width of rectangle: "))

print("The total area of circle is: ",circle.circ_area(radius))
print("The total area of rectangle is: ",rect.rect_area(length,width))
print("The circumference of circle is: ",circle.circ_circum(radius))
print("The perimeter of rectangle is: ",rect.rect_perim(length,width))

Output:

Enter the radius of the circle: 5 
Enter the length of rectangle: 10
Enter the width of rectangle: 4
Traceback (most recent call last):
  File "c:\Desktop\geometry.py", line 7, in <module>
    print("The total area of circle is: ",circle.circ_area(radius))
  File "c:\Desktop\circle.py", line 8, in circ_area  
    return self.radius**2*3.14
AttributeError: 'int' object has no attribute 'radius'

One note, Careful about this notation:

self.radius**2*3.14

since it’s common for languages to not know what you mean (python3 and python2 for example will give you different interpretations. Explicit parenthesis help readability and ensure it’ll be the number you want.

As to your error: Check the names of your rectangle variables.

Also I’m assuming you are indenting your class methods in and it just pasted strange. Friendly reminder that class methods have to be nested inside the class :slight_smile:

I tried indenting it so I think it helps a bit but I got another error in the geometry.py that it can’t find circ_area, How do i fix it?

AttributeError: module 'circle' has no attribute 'circ_area'

You have to instantiate the class, it’s not enough to import it.

If it were in the same file you might do

class Foo:
    def bar():
        #method code

#outside of the class

f = Foo()
f.bar()
2 Likes

Okay I get it now, but how do I import that to another script which has import circle which in my case, the geometry.py?

Your import is fine. It’s importing the class. Depending on how you import you might have to do a little extra work:

class Foo:
    def bar(self):
        print("baz")

in another file

from fooBarBaz import Foo

f = Foo()
f.bar()
#prints "baz"
1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.