質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

2回答

1229閲覧

【python3】エラーの原因が分からないです

uc200

総合スコア32

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2017/10/15 14:46

現在『楽しいプログラミング pythonで始めよう』という本でpython3を勉強中です。そこで出てくるコードに次のようなものがあります。

python3.

1from tkinter import * 2import random 3import time 4 5class Game: 6 def __init__(self): 7 self.tk=Tk() 8 self.tk.title("Mr. Stick Man Races for the Exit") 9 self.tk.resizable(0,0) 10 #self.tk.wm_attributes("-topmost",1) 11 self.canvas = Canvas(self.tk, width=500, height=500, highlightthickness=0) 12 self.canvas.pack() 13 self.tk.update() 14 self.canvas_height=500 15 self.canvas_width=500 16 self.bg=PhotoImage(file="background.gif") 17 self.bg2=PhotoImage(file="background2.gif") 18 self.bg3=PhotoImage(file="background3.gif") 19 self.bg4=PhotoImage(file="background4.gif") 20 w = self.bg.width() 21 h = self.bg.height() 22 for x in range(0,5): 23 for y in range(0,5): 24 self.canvas.create_image(w+x*w*2, y*h*2, image=self.bg, anchor="nw") 25 self.canvas.create_image(x*w*2, h+y*h*2, image=self.bg, anchor="nw") 26 self.canvas.create_image(x*w*2, y*h*2, image=self.bg2, anchor="nw") 27 self.canvas.create_image(w+x*w*2, h+y*h*2, image=self.bg2, anchor="nw") 28 self.canvas.create_image(w*4, h*2, image=self.bg3, anchor="nw") 29 self.canvas.create_image(w*4, h*4, image=self.bg4, anchor="nw") 30 self.sprites=[] 31 self.running = True 32 33 def mainloop(self): 34 while 1: 35 if self.running == True: 36 for sprite in self.sprites: 37 sprite.move() 38 self.tk.update_idletasks() 39 self.tk.update() 40 time.sleep(0.01) 41 42 43class Coords: 44 def __init__(self, x1=0, y1=0, x2=0, y2=0): 45 self.x1 = x1 46 self.y1 = y1 47 self.x2 = x2 48 self.y2 = y2 49 50def within_x(co1,co2): 51 if (co1.x1>co2.x1 and co1.x1<co2.x2)\ 52 or (co1.x2>co2.x1 and co1.x2<co2.x2)\ 53 or (co2.x1>co1.x1 and co2.x1<co1.x2)\ 54 or (co2.x2>co1.x1 and co1.x2<co1.x1): 55 return True 56 else: 57 return False 58 59 60 61def within_y(co1,co2): 62 if (co1.y1>co2.y1 and co1.y1<co2.y2)\ 63 or (co1.y2>co2.y1 and co1.y2<co2.y2)\ 64 or (co2.y1>co1.y1 and co2.y1<co1.y2)\ 65 or (co2.y2>co1.y1 and co2.y2<co1.y1): 66 return True 67 else: 68 return False 69 70def collided_left(co1,co2): 71 if within_y(co1,co2): 72 if co1.x1 <= co2.x2 and co1.x1 >= co2.x1: 73 return True 74 return False 75 76def collided_right(co1,co2): 77 if within_y(co1,co2): 78 if co1.x2 <= co2.x1 and co1.x2 >= co2.x2: 79 return True 80 return False 81 82def collided_top(co1,co2): 83 if within_x(co1,co2): 84 if co1.y1 <= co2.y2 and co1.y1 >= co2.y1: 85 return True 86 return False 87 88def collided_bottom(y,co1,co2): 89 if within_x(co1,co2): 90 y_calc = co1.y2 + y 91 if y_calc <= co2.y1 and y_calc >= co2.y2: 92 return True 93 return False 94 95class Sprite: 96 def __init__(self,game): 97 self.game = game 98 self.endgame = False 99 self.coordinates = None 100 101 def move(self): 102 pass 103 104 def coords(self): 105 return self.coordinates 106 107class PlatformSprite(Sprite): 108 def __init__(self, game, photo_image, x, y, width, height): 109 Sprite.__init__(self,game) 110 self.photo_image = photo_image 111 self.image = game.canvas.create_image(x,y,image=self.photo_image,anchor="nw") 112 self.coordinates = Coords(x,y,x+width,y+height) 113 114class StickFigureSprite(Sprite): 115 def __init__(self,game): 116 Sprite.__init__(self,game) 117 self.images_left=[ 118 PhotoImage(file="figure-L1.gif"), 119 PhotoImage(file="figure-L2.gif"), 120 PhotoImage(file="figure-L3.gif") 121 ] 122 self.images_right=[ 123 PhotoImage(file="figure-R1.gif"), 124 PhotoImage(file="figure-R2.gif"), 125 PhotoImage(file="figure-R3.gif") 126 ] 127 self.image=game.canvas.create_image(200,470,\ 128 image=self.images_left[0],anchor="nw") 129 self.x=-2 130 self.y=0 131 self.current_image=0 132 self.current_image_add=1 133 self.jump_count=0 134 self.last_time=time.time() 135 self.coordinates=Coords() 136 game.canvas.bind_all("<KeyPress-Left>",self.turn_left) 137 game.canvas.bind_all("<KeyPress-Right>",self.turn_right) 138 game.canvas.bind_all("<space>",self.jump) 139 140 def turn_left(self,evt): 141 if self.y==0: 142 self.x=-2 143 144 def turn_right(self,evt): 145 if self.y==0: 146 self.x=2 147 148 def jump(self,evt): 149 if self.y==0: 150 self.y=-4 151 self.jump_count=0 152 153 def animate(self): 154 if self.x !=0 and self.y==0: 155 if time.time()-self.last_time>0.1: 156 self.last_time=time.time() 157 self.current_image += self.current_image_add 158 if self.current_image>=2: 159 self.current_image_add<=-1 160 if self.current_image<=0: 161 self.current_image_add=1 162 if self.x<0: 163 if self.y != 0: 164 self.game.canvas.itemconfig(self.image,\ 165 image=self.images_left[2]) 166 else: 167 self.game.canvas.itemconfig(self.image,\ 168 image=self.images_left[self.current_image]) 169 if self.x>0: 170 if self.y !=0: 171 self.game.canvas.itemconfig(self.image,\ 172 image=self.images_right[2]) 173 else: 174 self.game.canvas.itemconfig(self.image,\ 175 image=self.images_right[self.current_image]) 176 177 def coords(self): 178 xy=self.game.canvas.coords(self.image) 179 self.coordinates.x1=xy[0] 180 self.coordinates.y1=xy[1] 181 self.coordinates.x2=xy[0]+27 182 self.coordinates.y2=xy[1]+30 183 184 def move(self): 185 self.animate() 186 if self.y<0: 187 self.jump_count +=1 188 if self.jump_count>20: 189 self.y=4 190 if self.y>0: 191 self.jump_count -=1 192 co=self.coords() 193 left=True 194 right=True 195 top=True 196 bottom=True 197 falling=True 198 if self.y>0 and co.y2>=self.game.canvas_height: 199 self.y=0 200 bottom=False 201 elif self.y<0 and co.y1<=0: 202 self.y=0 203 top=False 204 if self.x>0 and co.x2>=self.game.canvas_width: 205 self.x=0 206 right=False 207 elif self.x < 0 and co.x1 <= 0: 208 self.x=0 209 left=False 210 for sprite in self.game.sprites: 211 if sprite == self: 212 continue 213 sprite_co=sprite.coords() 214 if top and self.y<0 and collided_top(co,sprite_co): 215 self.y=-self.y 216 top=False 217 if bottom and self.y>0 and collided_bottom(self.y,\ 218 co,sprite_co): 219 self.y=sprite_co.y1-co.y2 220 if self.y<0: 221 self.y=0 222 bottom=False 223 top=False 224 if bottom and falling and self.y==0\ 225 and co.y2<self.game.canvas_height\ 226 and collided_bottom(1,co,sprite_co): 227 falling=False 228 if left and self.x<0 and collided_left(co,sprite_co): 229 self.x=0 230 left=False 231 if right and self.x>0 and collided_right(co,sprite_co): 232 self.x=0 233 right=False 234 if falling and bottom and self.y==0\ 235 and co.y2<self.game.canvas_height: 236 self.y=4 237 self.game.canvas.move(self.image,self.x,self.y) 238 239 240g=Game() 241platform1=PlatformSprite(g,PhotoImage(file="platform1.gif"),0,480,100,10) 242platform2=PlatformSprite(g,PhotoImage(file="platform1.gif"),150,440,100,10) 243platform3=PlatformSprite(g,PhotoImage(file="platform1.gif"),300,400,100,10) 244platform4=PlatformSprite(g,PhotoImage(file="platform1.gif"),300,160,100,10) 245platform5=PlatformSprite(g,PhotoImage(file="platform2.gif"),175,350,66,10) 246platform6=PlatformSprite(g,PhotoImage(file="platform2.gif"),50,300,66,10) 247platform7=PlatformSprite(g,PhotoImage(file="platform2.gif"),170,120,66,10) 248platform8=PlatformSprite(g,PhotoImage(file="platform2.gif"),45,60,66,10) 249platform9=PlatformSprite(g,PhotoImage(file="platform3.gif"),170,250,32,10) 250platform10=PlatformSprite(g,PhotoImage(file="platform3.gif"),230,200,32,10) 251 252g.sprites.append(platform1) 253g.sprites.append(platform2) 254g.sprites.append(platform3) 255g.sprites.append(platform4) 256g.sprites.append(platform5) 257g.sprites.append(platform6) 258g.sprites.append(platform7) 259g.sprites.append(platform8) 260g.sprites.append(platform9) 261g.sprites.append(platform10) 262 263sf=StickFigureSprite(g) 264g.sprites.append(sf) 265g.mainloop() 266

しかし、これを実行すると、
AttributeError: 'NoneType' object has no attribute 'x1'
とエラーが出ます。207行目の
elif self.x < 0 and co.x1 <= 0:
という所がおかしいらしいのですが、どこが間違っているのかよく分かりません。エラーが意味するところもよく分かりません。

どなたか分かる方ご教授ください。
よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

そのエラーメッセージは co.x1 を参照しようとしたとき発生しており、内容は英語そのまんまですが「co には x1 なんてないよ。ちなみに co の型は NoneType ですよ」という意味です。

co が None (他の言語でいう null) なのは明らかにおかしいので、今回のエラーが発生した場所ではなく、co を格納した部分に問題がありそうです。

ではその co はどこから来てるというと、192行目付近の co = self.coords()self.coords() の結果を格納しています。

python

1 co=self.coords()

では self.coords() は何を返しているかを追ってみると、177行目の def coords(self):... で始まる関数がそれです。

python

1 def coords(self): 2 xy=self.game.canvas.coords(self.image) 3 self.coordinates.x1=xy[0] 4 self.coordinates.y1=xy[1] 5 self.coordinates.x2=xy[0]+27 6 self.coordinates.y2=xy[1]+30

ご覧の通り、return 文が無く何も返していません。co には常に None が格納されます。すなわちこのサンプルコードは絶対に正常に動きません


技術書のサンプルコードが正常に動かない場合、まず出版社のサイトで訂正情報が出ていないか確認しましょう。無い場合は出版社に問い合わせてみるのもよいと思います。


最後に一般論としてプログラムがエラーで止まってしまう場合、まずエラーメッセージを読みましょう。英語が苦手であれば機械翻訳でも十分理解できます。で、そのメッセージに出ている場所/関数/変数をソースコードから探し、動きを逆に追っていきましょう。

投稿2017/10/15 15:09

編集2017/10/15 15:11
miyahan

総合スコア3095

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

uc200

2017/10/16 13:37

詳細な回答ありがとうございます! 調べてみたら本ではなく私が間違っておりました。
guest

0

192行目に次の記述があります。

Python

1co=self.coords()

coordsメソッドの中身はこちら。

Python

1def coords(self): 2 xy=self.game.canvas.coords(self.image) 3 self.coordinates.x1=xy[0] 4 self.coordinates.y1=xy[1] 5 self.coordinates.x2=xy[0]+27 6 self.coordinates.y2=xy[1]+30

coordsメソッドが何か値を返す必要があるのでは?
ちょっと確認してみてください。

投稿2017/10/15 14:58

LouiS0616

総合スコア35660

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

uc200

2017/10/16 13:37

回答ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問