-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:リスト割り当てインデックスが範囲外です。 "
:
はこれを試してみてください? –
インデックスを誤って使用しています。 'up、down、left、right'は' len(directions) 'より大きい値を持つ値です。例えば 'down = randint(10,20)'は 'len(directions)'が4で 'down> len(directions)'を意味し、 'down> = 10'を意味します。 –