attribute error in python (code-skulpor)
我试图在codeskulptor上运行以下代码。它给出了一个错误:第17行:attributeError:'card'对象没有属性'rank'。是这样吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import simplegui ranks=('A','2','3','4','5','6','7','8','9','T','J','Q','K') suits=('C','S','H','D') card_centre=(36.5,49) card_size=(73,98) tiled_image=simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/cards.jfitz.png") class card: def _init_(self,suit,rank): self.rank=rank self.suit=suit def draw(self,canvas,loc): global ranks,suits i=ranks.index(self.rank) j=suits.index(self.suit) card_pos=[card_centre[0]+i*card_size[0],card_centre[1]+j*card_size[1]] canvas.draw_image(tiled_image,card_pos,card_size,loc,card_size) def draw(canvas): one_card.draw(canvas,[300,200]) frame=simplegui.create_frame("Card draw",600,400) frame.set_draw_handler(draw) one_card=card('H','J') frame.start() |
不确定这是否真的是问题所在,但您的
1 2 3 | def __init__(self, suit, rank): self.rank = rank self.suit = suit |
注意,它有两个
但是,如果这是问题所在,那么您甚至应该在这之前得到另一个错误,即