- 我一直在尋找使用類方法創建圓圈的不同方法。起初,我能夠列印'c1',但是,在更新我的python之后,它說沒有定義這讓我感到困惑。
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)
--------------------------------------------------------------
from circle.Circle import move
c1_xdelta = 4
c1_ydelta = 5
for i in range(1, 20):
if i%10 == 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%10 == 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%10 == 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'
有什么方法可以解決它或任何推薦的網站來實作這種編程功能?謝謝你。
uj5u.com熱心網友回復:
這里有一些問題。首先一個類不接受任何引數,所以而不是
class Circle(object):
它只是
class circle
其次,您使用一個下劃線表示__init__,所以它是
class Circle:
def __init__(x0, y0, r):
你也沒有將宣告的變數設定為任何東西,它必須是這個
class Circle:
def __init__(x0, y0, r):
self.x0 = x0
self.y0 = y0
self.r = r
變數名最好不要使用大寫,所以把 R 改成 r
您還將 c1、c2、c3 和 c4 變數縮進到類中,它必須是這個
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)
現在您可以列印出 c1。但是 c1 只會列印出類似<__main__.circle object at 0x000001F89906A0A0>的內容,因為您正在列印物件本身。如果要列印特定值,請使用 . 運營商,所以它會是
print(c1.x0)
那會列印x0變數
如果你想列印所有你可以使用的
print(c1.x0, c1.y0, c1.r)
這是否符合您的目標?
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/437075.html
上一篇:鏈表搜索功能無法正常作業
下一篇:創建一個回傳物件的函式
