问题描述
我正在尝试使用if
语句调用列表搜索,然后将值添加到可以调用以在turtle中绘制对象的坐标。欢迎使用类似的解决方案,但我真的很想知道我在这里的想法出在哪里:
fixed_data_set_01 = [['Competitor A','Right'],['Competitor A','Down'],'Left'],'Up']]
def list_searcher():
global fixed_data_set_01
d = len(fixed_data_set_01)
f = 0 # looping variable to search through index
y = 0 # defining y coord
x = 0 # defining x coord
for lister in range(d):
print(fixed_data_set_01[f])
f = f + 1
p = fixed_data_set_01[f]
if 'Up' in p:#if up is in the move set add value to y coord for competitor
y = y + 90
print(y,x)# testing
return y
if 'Down' in p:#if down is in the move set add value to y coord for competitor
y = y - 90
print(y,x)# testing
return y
if 'Left' in p:#if left is in the move set add value to x coord for competitor
x = x - 120
print(y,x)
return x
if 'Right' in p:#if right is in the move set add value to x coord for competitor
x = x + 120
print(y,x) # testing
return x
list_searcher()
解决方法
我很难理解您的代码和解释。我能想到的最好的办法是,数据集应生成竞争对手遵循的路径,因此我在下面实现了该路径:
from turtle import Screen,Turtle
fixed_data_set_01 = [
['Competitor A','Right'],['Competitor A','Down'],'Left'],'Up'],]
def list_searcher(competitor,data_set):
path = [(0,0)] # starting coordinates
for who,*directions in data_set:
if who != competitor:
continue
x,y = path[-1]
if 'Up' in directions: # if Up is in the set add,value to y coord
path.append((x,y + 90))
elif 'Down' in directions: # if Down is in the set,subtract value from y coord
path.append((x,y - 90))
elif 'Left' in directions: # if Left is in the set,subtract value from x coord
path.append((x - 120,y))
elif 'Right' in directions: # if Right is in the set,add value to x coord
path.append((x + 120,y))
return path
screen = Screen()
path = list_searcher('Competitor A',fixed_data_set_01)
turtle = Turtle()
for position in path:
turtle.goto(position)
screen.exitonclick()
如果不是 情况,请提供更多代码并详细说明您要执行的操作。