2017-10-04 7 views
-1

このプログラムの機能は、40 x 40グラフ上で使用する変数を右から左、上から下へと1から10の範囲の数値をランダムに生成することです。 xとy変数のための2つの数値を含むリストを持っているために、私はこのコードを実行しようとするたびグラフ上で使用する整数を無作為に生成する

import random 
choice_x = random.randint(0, 1) 
choice_y = random.randint(0, 1) 
right = random.randint(10, 20) 
left = random.randint(0, 10) 
up = left = random.randint(0, 10) 
down = left = random.randint(10, 20) 
directions = [right, left, up, down] 

if choice_x == 0: 
    del directions[right] 
elif choice_x == 1: 
    del directions[left] 

if choice_y == 0: 
    del directions[up] 
elif choice_y == 1: 
del directions[down] 
print (directions) 

を選択されるピック別の乱数ジェネレータを持って、私は "というメッセージを得続けますIndexError:リスト割り当てインデックスが範囲外です。 "

+0

はこれを試してみてください? –

+0

インデックスを誤って使用しています。 'up、down、left、right'は' len(directions) 'より大きい値を持つ値です。例えば ​​'down = randint(10,20)'は 'len(directions)'が4で 'down> len(directions)'を意味し、 'down> = 10'を意味します。 –

答えて

1

ifステートメント内にエラーがあります。 directionsリストにはインデックスを作成していません。変数(rightleftupdown)のインデックスを作成しようとしています。そのため、エラーです。あなたは `デル方向[右]`をどうするかをしようとしている

if choice_x == 0: del directions[0] 
elif choice_x == 1: del directions[1] 

if choice_y == 0: del directions[2] 
elif choice_y == 1: del directions[3] 
関連する問題