This is the question: create a class in Python for circle called Circle such as its constructor can take zero, one or two or three parameters as shown below: create a unit circle centered at (0,0) c1 = Circle() # create a circle of radius 3 centered at (0,0) c2 = Circle(3) # create a unit circle centered at (2, 5) c3 = Circle(2,5) # create a circle of radius 3 centered at (4, 2) c4 = Circle(3,4,2)
- I have been searching for different methods for using a class method to create circles. At first, I was able to print 'c1', however, after updating my python, it said is not defined which confuses me.
def __int__(x0, y0, r):
self.x0 = x0
self.y0 = y0
self.r = r
# create a unit circle centered at (0,0)
c1 = Circle(0, 0, 1)
# create a circle of radius 3 centered at (0,0)
c2 = Circle(0, 0, 3)
# create a unit circle centered at (2, 5)
c3 = Circle(2, 5, 1)
# create a circle of radius 3 centered at (4, 2)
c4 = Circle(3, 4, 2)
print (c1)
--------------------------------------------------------------
- This is the second part of my question: translate the center of a circle by shifting it along x- and y-axes, e.g. c1.shift(4,5) #the unit circle is now centered at (4,5). However, I was unable to continue my programming as my python shows 'ModuleNotFoundError: No module named 'circle''. However, I have already updated my pip and it does not allow me to download the 'circle' module.
from circle.Circle import move
c1_xdelta = 4
c1_ydelta = 5
for i in range(1, 20):
if i == 0:
c1.move(c1_xdelta, c1_ydelta)
from circle.Circle import move
c1_xdelta = 4
c1_ydelta = 5
for i in range(1, 20):
if i == 0:
c1.move(c1_xdelta, c1_ydelta)
from circle.Circle import move
c1_xdelta = 4
c1_ydelta = 5
for i in range(1, 20):
if i == 0:
c1.move(c1_xdelta, c1_ydelta)
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent
call last)
~\AppData\Local\Temp/ipykernel_7076/1685247526.py in <module>
----> 1 from circle.Circle import move
2 c1_xdelta = 4
3 c1_ydelta = 5
4
5 for i in range(1, 20):
ModuleNotFoundError: No module named 'circle'
Is there any method that I can solve it or any recommended websites for such programming functions? Thank you.
CodePudding user response:
There's a few things wrong here. First off a class doesn't take in any arguments, so instead of
class Circle(object):
it would just be
class circle
secondly you are using one underscore for init, so it'd be
class Circle:
def __init__(x0, y0, r):
also you aren't setting the declared variables to anything, it needs to be this
class Circle:
def __init__(x0, y0, r):
self.x0 = x0
self.y0 = y0
self.r = r
it's also best not to use capitals for variable names, so change R to r
you've also indented the c1, c2, c3 and c4 variables to be inside the class, it needs to be this
class Circle:
def __init__(x0, y0, r):
self.x0 = x0
self.y0 = y0
self.r = r
c1 = Circle(0, 0, 1)
c2 = Circle(0, 0, 3)
c3 = Circle(2, 5, 1)
c4 = Circle(3, 4, 2)
now you can print out c1. But c1 will just print out something like "<main.circle object at 0x000001F89906A0A0>" because you're printing the object itself. If you want to print a specific value you use the . operator, so it'd be
print(c1.x0)
that'd print the x0 variable
if you want to print them all you could use
print(c1.x0, c1.y0, c1.r)
is this something along the lines of what you're going for?
class circle:
def __init__(self, x0, y0, r):
self.x0 = x0
self.y0 = y0
self.r = r
def move(self, x, y):
self.x0 = x
self.y0 = y
c1 = circle(0, 0, 1)
c1_xdelta = 4
c1_ydelta = 5
for i in range(1, 20):
if i % 10 == 0:
c1.move(c1_xdelta, c1_ydelta)
print(c1.x0, c1.y0)