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

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

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

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

Q&A

解決済

1回答

7685閲覧

tuple index out of rangeのエラー

suugaku_nyumon

総合スコア37

Python

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

0グッド

0クリップ

投稿2018/12/02 17:23

編集2018/12/02 17:25

前提・実現したいこと

Pythonでコンウェイのライフゲームを見てみようと思いました。そして、計算物理学Ⅱという本のセルオートマトンの所に書かれているコードを入力した所、うまくいかなかったので、エラーメッセージを見ながら直していきましたが、「tuple index out of range」のエラーでつまずきました。

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

エラーメッセージ

Traceback (most recent call last): File "GameOfLife.py", line 8, in <module> [(-49 , -49) , (-49 , 49) , (49 , 49) , (49 , -49) , (-49 , -49)] , color = color.white) File "C:\Python37\lib\site-packages\vpython\vpython.py", line 1996, in __init__ self.append(tpos) File "C:\Python37\lib\site-packages\vpython\vpython.py", line 1852, in append pts, cps = self.process_args(*args1, **args) File "C:\Python37\lib\site-packages\vpython\vpython.py", line 1792, in process_args tpos = self.parse_pos(args1[0]) File "C:\Python37\lib\site-packages\vpython\vpython.py", line 1837, in parse_pos v = list_to_vec(v) File "C:\Python37\lib\site-packages\vpython\vpython.py", line 225, in list_to_vec return vector(L[0], L[1], L[2]) IndexError: tuple index out of range

該当のソースコード

Python 3.7.1

from vpython import * from IPython.display import display from numpy import zeros scene = display(width = 500 , height = 500 , title = 'Game of Life') cell = zeros((50 , 50)); cellu = zeros((50 , 50)) curve(pos = [(-49 , -49) , (-49 , 49) , (49 , 49) , (49 , -49) , (-49 , -49)] , color = color.white) boxes = points(shape = 'square' , size = 8 , color = color.cyan) def drawcells(ce): boxes.pos = [] for j in ranges(0 , 50): for i in range(0 , 50): if ce[i , j] == 1: xx = 2*i - 50 yy = 2*j - 50 boxes.append(pos = (xx , yy)) def initial(): for j in range(20 , 28): for i in range(20 , 28): r= int(random.random() * 2) cell[j , i] = r return cell def gameoflife(call): for i in range(1 , 49): for j in range(1 , 49): sum1 = cell[i-1 , j-1] + cell[i , j-1] + cell[i+1 , j-1] sum2 = cell[i-1 , j] + cell[i+1 , j] + cell[i-1 , j+1] + cell[i , j+1] + cell[i+1 , j+1] alive = sum1 + sum2 if cell[i , j] == 1: if alive == 2 or alive == 3: cellu[i , j] = 1 if alive > 3 or alive < 2: cellu[i , j] = 0 if cell[i , j] == 0: if alive == 3: cellu[i , j] = 1 else: cellu[i , j] = 0 alive = 0 return cellu temp = initial() drawcells(temp) while True: rate(6) cell = temp temp = gameoflife(cell) drawcells(cell)

試したこと

index out of rangeはindexは範囲の外という意味までは分かり、自分も探してみましたが、どこがおかしいのか分かりません。

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

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

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

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

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

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

can110

2018/12/02 17:45

提示ソースは本に載っているものそのままでしょうか、それともご自身で直した後のものでしょうか?
suugaku_nyumon

2018/12/02 22:34

本に載っているものを少し変更したものです。本に載っているものをそのまま実行しようとすると、エラーが出てきたので変えました。例えば、最初はfrom visual import *でしたが、visualでは動作せずvpythonに変更しました。
can110

2018/12/02 23:53

追記ありがとうございます。サポートページ見て、紹介されているコンバーターも試してみましたが、最新Verへの対応は難易度高いです。
suugaku_nyumon

2018/12/03 00:31

ご回答ありがとうございました。初心者が正しいコードに変えるのは難しそうなので、一旦このコードは諦めます。本当にありがとうございました。
guest

回答1

0

ベストアンサー

エラーメッセージによるとタプル(-49 , -49) などの3番目の要素にアクセスしようとして提示エラーが発生していると思われます。
マニュアルのcurveを確認すると、posとして与えるタプルの要素数は3つでないといけないようです(3次元だから)。
よって、以下のように修正することで

Python

1curve( pos = 2color.white) 3 [(-49 , -49, 0) , (-49 , 49, 0) , (49 , 49, 0) , (49 , -49, 0) , (-49 , -49, 0)] , color = color.white)

以下の画像が得られました。
イメージ説明
ただしすぐに次の行で
AttributeError: 'points' object has no attribute '_size'
が発生してしまいます。
ほかエラーをいくつか潰してみましたが、死滅セルの更新ができず、まともに動作させることはできませんでした。

Python

1from vpython import * 2from IPython.display import display 3from numpy import zeros 4import random 5 6scene = display(width = 500 , height = 500 , title = 'Game of Life') 7cell = zeros((50 , 50)); cellu = zeros((50 , 50)) 8curve( pos = 9 [(-49 , -49, 0) , (-49 , 49, 0) , (49 , 49, 0) , (49 , -49, 0) , (-49 , -49, 0)] , color = color.white) # *** 10boxes = points(shape = 'square' , color = color.cyan) 11 12def drawcells(ce): 13 boxes.empty() # *** 14 for j in range(0 , 50): 15 for i in range(0 , 50): # *** 16 if ce[i , j] == 1: 17 xx = 2*i - 50 18 yy = 2*j - 50 19 boxes.append(pos = (xx , yy, 0)) # *** 20 21def initial(): 22 for j in range(20 , 28): 23 for i in range(20 , 28): 24 r= int(random.random() * 2) 25 cell[j , i] = r 26 return cell 27 28def gameoflife(call): 29 for i in range(1 , 49): 30 for j in range(1 , 49): 31 sum1 = cell[i-1 , j-1] + cell[i , j-1] + cell[i+1 , j-1] 32 sum2 = cell[i-1 , j] + cell[i+1 , j] + cell[i-1 , j+1] + cell[i , j+1] + cell[i+1 , j+1] 33 alive = sum1 + sum2 34 if cell[i , j] == 1: 35 if alive == 2 or alive == 3: 36 cellu[i , j] = 1 37 if alive > 3 or alive < 2: 38 cellu[i , j] = 0 39 if cell[i , j] == 0: 40 if alive == 3: 41 cellu[i , j] = 1 42 else: 43 cellu[i , j] = 0 44 alive = 0 45 return cellu 46temp = initial() 47drawcells(temp) 48while True: 49 rate(6) 50 cell = temp 51 temp = gameoflife(cell) 52 drawcells(cell)

イメージ説明

投稿2018/12/02 17:52

編集2018/12/02 18:11
can110

総合スコア38266

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問