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

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

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

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

Python

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

Q&A

解決済

3回答

1051閲覧

Pythonのエラー文について

退会済みユーザー

退会済みユーザー

総合スコア0

Python 3.x

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

Python

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

0グッド

0クリップ

投稿2018/01/19 00:48

def main():
try:
init() # Grid Eye 初期化
while True:
try:
temp = read_block(0x0e, 2) # サーミスタ温度(0x0e から 2バイト)
s_temp = calc_temp1(temp)[0] # サーミスタ温度計算
data0 = 'Thermistor:{0}'.format(s_temp) # サーミスタ温度(℃)
time.sleep(0.5)
temp0 = read_block(0x80) # 0x80 から 32バイト読込
temp1 = read_block(0xa0) # 0xa0 から 32バイト読込
temp2 = read_block(0xc0) # 0xc0 から 32バイト読込
temp3 = read_block(0xe0) # 0xe0 から 32バイト読込
temp = temp3 + temp2 + temp1 + temp0 # 受信データリスト連結
d0 = calc_temp2(temp0) # 温度計算
d1 = calc_temp2(temp1)
d2 = calc_temp2(temp2)
d3 = calc_temp2(temp3)
data1 = d3 + d2 + d1 + d0 # 温度レジスタ(℃)
wData = '{0}\n{1}'.format(data0, data1)
list01 = data1.split(",+")
del list01[63]
del list01[0]
list02 = list(map(float,list01))
time.sleep(0.1)

temp = read_block(0x0e, 2) # サーミスタ温度(0x0e から 2バイト) s_temp = calc_temp1(temp)[0] # サーミスタ温度計算 data0 = 'Thermistor:{0}'.format(s_temp) # サーミスタ温度(℃) time.sleep(0.5) temp0 = read_block(0x80) # 0x80 から 32バイト読込 temp1 = read_block(0xa0) # 0xa0 から 32バイト読込 temp2 = read_block(0xc0) # 0xc0 から 32バイト読込 temp3 = read_block(0xe0) # 0xe0 から 32バイト読込 temp = temp3 + temp2 + temp1 + temp0 # 受信データリスト連結 d0 = calc_temp2(temp0) # 温度計算 d1 = calc_temp2(temp1) d2 = calc_temp2(temp2) d3 = calc_temp2(temp3) data1 = d3 + d2 + d1 + d0 # 温度レジスタ(℃) wData = '{0}\n{1}'.format(data0, data1) list03 = data1.split(",+") del list03[63] del list03[0] list04 = list(map(float,list03)) print(list02) print(list04) i = 0 sabun = list() for i in range(len(list04)): if list02[i] > list04[i]: sabun[i] = list02[i] - list04[i] i += 1 elif list02[i] <= list04[i]: sabun[i] = list04[i] - list02[i] i += 1 print(sabun) goukei = sum(sabun) print(goukei) time.sleep(0.1) except Exception as e: print('main:{0}'.format(e)) time.sleep(5) except Exception as e: print('main:{0}'.format(e))

==============================================

if name=='main':
main()

これを実行するとmain:list assignment index out of rangeというエラーが出ます
解決方法を教えてください

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

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

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

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

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

LouiS0616

2018/01/19 06:43

@quiqui さん どうやらこれが『エラーの全文』のようですよ。except Exceptionなんてしたせいで...
guest

回答3

0

ベストアンサー

既にLhankor_Mhyさんが指摘されているとおりに、次の部分がおかしいです。

Python

i = 0
sabun = list()
for i in range(len(list04)):
if list02[i] > list04[i]:
sabun[i] = list02[i] - list04[i]
i += 1
elif list02[i] <= list04[i]:
sabun[i] = list04[i] - list02[i]
i += 1

エラーの有無を超えて、次の三点の問題があります。

  • リストの使い方がおかしい

C言語などの配列と根本的に異なる要素です。

  • iの取り扱いがおかしい

前もってi=0を書く必要はないですし、i+=1もおそらく意図通りでなく無駄です。

  • 条件分岐の書き方がおかしい

排他的な条件なのですから、if-elifではなくif-elseで書けばよいです。
また、正の差分(距離)を取りたいのならそもそも条件分岐は不要です。

以上の点を踏まえて、『Pythonらしく』書くと次のようになります。

Python

1sabun = [] 2for e1, e2 in zip(list02, list04): 3 sabun.append(abs(e1-e2))

内包表記を用いると、さらに簡潔に書けます。しかもこのケースでは高速化も望めます。

Python

1sabun = [abs(e1-e2) for e1, e2 in zip(list02, list04)]

これだけです。


また、例外の処理も気になります。

Python

1>>> [][3] 2Traceback (most recent call last): 3 File "<stdin>", line 1, in <module> 4IndexError: list index out of range 5>>> 6>>> try: 7... [][3] 8... except Exception as e: 9... print(e) 10... 11list index out of range

下手に処理しようとした結果、むしろ情報量が減っています。
少なくともExceptionで捉えようとするのはやめましょう。


いろいろと無駄なコードが多い印象です。
もうちょっと基礎的な部分を抑えれば、エラーも自然と減るはずですよ。

投稿2018/01/19 06:41

編集2018/01/19 11:57
LouiS0616

総合スコア35658

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

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

0

python

1sabun = list() 2for i in range(len(list04)): 3 if list02[i] > list04[i]: 4 sabun[i] = list02[i] - list04[i]

sabunは空リストだから、これはエラーになるのでは。

投稿2018/01/19 05:45

Lhankor_Mhy

総合スコア35865

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

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

0

どこかで配列の外をアクセスしようとしていることはわかりました。
どの行でエラーが出ているのかはpythonのインタプリタが教えてくれるので、そこをみると良いのではないでしょうか?

投稿2018/01/19 04:33

mkgrei

総合スコア8560

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問