私はこのコードを使用して、特定の位置が2回表示されているかどうかを判断するループ内にループを作成しようとしています。値を変更する
current_dir = 1
position = [0,0]
index = 0
move_list = ["L2", "L3", "L3", "L4", "R1", "R2", "L3", "R3", "R3", "L1", "L3", "R2", "R3", "L3", "R4", "R3", "R3", "L1", "L4", "R4", "L2", "R5", "R1", "L5", "R1", "R3", "L5", "R2", "L2", "R2", "R1", "L1", "L3", "L3", "R4", "R5", "R4", "L1", "L189", "L2", "R2", "L5", "R5", "R45", "L3", "R4", "R77", "L1", "R1", "R194", "R2", "L5", "L3", "L2", "L1", "R5", "L3", "L3", "L5", "L5", "L5", "R2", "L1", "L2", "L3", "R2", "R5", "R4", "L2", "R3", "R5", "L2", "L2", "R3", "L3", "L2", "L1", "L3", "R5", "R4", "R3", "R2", "L1", "R2", "L5", "R4", "L5", "L4", "R4", "L2", "R5", "L3", "L2", "R4", "L1", "L2", "R2", "R3", "L2", "L5", "R1", "R1", "R3", "R4", "R1", "R2", "R4", "R5", "L3", "L5", "L3", "L3", "R5", "R4", "R1", "L3", "R1", "L3", "R3", "R3", "R3", "L1", "R3", "R4", "L5", "L3", "L1", "L5", "L4", "R4", "R1", "L4", "R3", "R3", "R5", "R4", "R3", "R3", "L1", "L2", "R1", "L4", "L4", "L3", "L4", "L3", "L5", "R2", "R4", "L2"]
for i in move_list:
turn = i[0]
movement = int(i[1:])
if turn == "R":
current_dir += 1
if current_dir > 4:
current_dir = 1
if turn == "L":
current_dir -= 1
if current_dir < 1:
current_dir = 4
if current_dir == 1: #Move North
position[1] += movement
if current_dir == 3: #Move South
position[1] -= movement
if current_dir == 2: #Move East
position[0] += movement
if current_dir == 4: #Move West
position[0] -= movement
position_2 = position
current_dir2 = current_dir
turn2 = turn
for f in move_list[index+1:]:
turn2 = f[0]
movement2 = int(f[1:])
if turn2 == "R":
current_dir2 += 1
if current_dir2 > 4:
current_dir2 = 1
if turn2 == "L":
current_dir2 -= 1
if current_dir2 < 1:
current_dir2 = 4
if current_dir2 == 1: # Move North
position_2[1] += movement2
if current_dir2 == 3: # Move South
position_2[1] -= movement2
if current_dir2 == 2: # Move East
position_2[0] += movement2
if current_dir2 == 4: # Move West
position_2[0] -= movement2
print position_2
print position
i = + 1
index += 1
それは正常に動作する必要がありますが、私はposition_2を変更しようとしていると私は本当に理由を理解していないときの位置の値が変更される問題を抱えています。
それは私がそれを考え出し、この部分での
if current_dir2 == 1: # Move North
position_2[1] += movement2
if current_dir2 == 3: # Move South
position_2[1] -= movement2
if current_dir2 == 2: # Move East
position_2[0] += movement2
if current_dir2 == 4: # Move West
position_2[0] -= movement2
また、あなたのコードで 'i = + 1'部分を検証してください。私は間違った論理を信じています。 'i'はもともとmove_listのメンバーですが、これに1を加えようとしています。 –
@VijayakumarUdupa:それは1つを追加するのではなく、1に置き換えています。ループで直ちに更新されるため、何も傷つけません。ほとんどの目的でノーオペレーションです。 – ShadowRanger
コードを誤って読み取っています。 –