0
我有这段代码,我试图在循环内部有一个循环来确定某个位置是否被窥视了两次。更改值
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代替它。不会伤害任何东西,因为'i'会立即被循环更新,这对于大多数目的来说是不可操作的。 – ShadowRanger
糟糕,无法正确读取代码。 –