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

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

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

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

6325閲覧

Python ライフゲーム TypeError 'int' object is not subscriptable

ShimpeiImai

総合スコア13

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/01/28 13:04

前提・実現したいこと

ライフゲームをPythonで作りたいです。
youtubeのゲヱム道場さんの動画を参考にしています。
以下のエラーの原因がわかりません。

発生している問題・エラーメッセージ

例外が発生しました: TypeError
'int' object is not subscriptable
File "C:\Users\python\game_of_life2.py", line 52, in getAdjacentLivesCount
count += get_key(CELL_TYPE, cells[current][y2][x2][0])
File "C:\Users\python\game_of_life2.py", line 95, in <module>
n = getAdjacentLivesCount(x, y)

該当のソースコード

python

1#! python3 2# game_of_life.py - Game of life 3 4import os, sys, random, time 5from msvcrt import getch 6 7FIELD_WIDTH = FIELD_HEIGHT = 20 8 9cursorX = cursorY = 0 10 11 12cells = [] 13cells2 = [] 14 15current = 0 16 17CELL_TYPE = { 18 0 : "・", 19 1 : "■" 20} 21 22for y in range(FIELD_HEIGHT): 23 cells2.append([]) 24 for x in range(FIELD_WIDTH): 25 cells2[y].append([]) 26 27for y in range(FIELD_HEIGHT): 28 for x in range(FIELD_WIDTH): 29 cells2[y][x].append(CELL_TYPE[0]) 30 31cells.append(cells2) 32cells.append(cells2) 33 34 35 36 37def get_key(d, val): 38 keys = [k for k, v in d.items() if v == val] 39 if keys: 40 return int(keys[0]) 41 return None 42 43def getAdjacentLivesCount(_x, _y): 44 count = 0 45 46 for y in range(-1, 2): 47 for x in range(-1, 2): 48 if (x is 0) and (y is 0): 49 continue 50 x2 = (FIELD_WIDTH + _x + x) % FIELD_WIDTH 51 y2 = (FIELD_HEIGHT + _y + y) % FIELD_HEIGHT 52 count += get_key(CELL_TYPE, cells[current][y2][x2][0]) 53 54 return count 55 56while True: 57 os.system('cls') 58 for y in range(FIELD_HEIGHT): 59 for x in range(FIELD_WIDTH): 60 if (x is cursorX) and (y is cursorY): 61 print('◎', end='') 62 else: 63 print(cells[current][y][x][0], end='') 64 """if (x is cursorX) and (y is cursorY): 65 print("@", end='') 66 else: 67 print(getAdjacentLivesCount(x, y), end='')""" 68 print("\n") 69 70 while True: 71 ans = chr(ord(getch())) 72 if ans == 'w': 73 cursorY = cursorY-1 74 break 75 elif ans == 's': 76 cursorY = cursorY+1 77 break 78 elif ans == 'a': 79 cursorX = cursorX-1 80 break 81 elif ans == 'd': 82 cursorX = cursorX+1 83 break 84 elif ans == 'q': 85 sys.exit() 86 elif ans == ' ': 87 if cells[current][cursorY][cursorX][0] is CELL_TYPE[0]: 88 cells[current][cursorY][cursorX] = CELL_TYPE[1] 89 else: 90 cells[current][cursorY][cursorX] = CELL_TYPE[0] 91 break 92 elif ans == '\r': 93 for y in range(FIELD_HEIGHT): 94 for x in range(FIELD_WIDTH): 95 n = getAdjacentLivesCount(x, y) 96 next = cells[current][y][x] 97 if(cells[current][y][x]): 98 if(n <= 1) or (n >= 4): 99 next = 0 100 else: 101 if(n == 3): 102 next = 1 103 cells[current^1][y][x] = next 104 current = current^1 105 break 106 107

補足情報(FW/ツールのバージョンなど)

Visual Code
Anaconda Prompt のインタプリタ?

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

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

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

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

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

guest

回答1

0

ベストアンサー

エラーメッセージの意味は、「整数値に対して添え字を付けている」(例えば、3[1] のような)と言うことです。よく見直しましょう。

投稿2019/01/28 13:10

otn

総合スコア84423

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

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

otn

2019/01/28 14:31

cells[current][y][x] に何を入れるのかが定まっていないようです。 0 か 1 がいいのでは? > count += get_key(CELL_TYPE, cells[current][y2][x2][0]) これに添え字の[0]を付けたのは、cells[current][y2][x2] に何が入っているつもりだったんでしょうか?
ShimpeiImai

2019/01/29 12:10

cells[current][y2][x2]には['・']か['■']が入ってるはずです。
otn

2019/01/29 14:11

それは間違ってます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問