Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 字典控制海龟';运动_Python_String_Function_Dictionary_Turtle Graphics - Fatal编程技术网

Python 字典控制海龟';运动

Python 字典控制海龟';运动,python,string,function,dictionary,turtle-graphics,Python,String,Function,Dictionary,Turtle Graphics,我是编程新手,我正在努力解决我从学校得到的一项任务。我必须构建一个函数,该函数使用turtle从string类型的参数(例如“fdltfd”-向前移动、向左移动和再次向前移动)中绘制一些东西。这些命令位于字典中,因此我必须将字符串中的元素与字典键进行比较。如果他们匹配,命令乌龟移动。我写的代码是: def execute(turtle, length, args, *cmd): map = {'fd': turtle.fd(length), 'lt': turtle.lt(args), 'bk

我是编程新手,我正在努力解决我从学校得到的一项任务。我必须构建一个函数,该函数使用turtle从string类型的参数(例如“fdltfd”-向前移动、向左移动和再次向前移动)中绘制一些东西。这些命令位于字典中,因此我必须将字符串中的元素与字典键进行比较。如果他们匹配,命令乌龟移动。我写的代码是:

def execute(turtle, length, args, *cmd):

map = {'fd': turtle.fd(length), 'lt': turtle.lt(args), 'bk': turtle.bk(length), 'rt': turtle.rt(args), 'nop':None}

for command in cmd:
  if command in map.keys():
      map[command]()

execute(bob, 50, 45, 'fdltfd' )
问题是海龟只做字典里的事情,向前、向后、向左和向右移动,它甚至不用看我的
for
循环

def execute(turtle, length, args, *cmd):

map = {'fd': turtle.fd(length), 'lt': turtle.lt(args), 'bk': turtle.bk(length), 'rt': turtle.rt(args), 'nop':None}

for command in cmd:
  if command in map.keys():
      map[command]()

execute(bob, 50, 45, 'fdltfd' )

你能不能给我一些想法,我如何才能使这项工作?或者如果我想对了?当然,不是这方面的代码:)…非常感谢您的代码中的具体问题:
cmd
参数前面的星号不正确:

def execute(turtle, length, args, *cmd):

map = {'fd': turtle.fd(length), 'lt': turtle.lt(args), 'bk': turtle.bk(length), 'rt': turtle.rt(args), 'nop':None}

for command in cmd:
  if command in map.keys():
      map[command]()

execute(bob, 50, 45, 'fdltfd' )
def execute(turtle, length, args, *cmd):
考虑到您调用它的方式:

def execute(turtle, length, args, *cmd):

map = {'fd': turtle.fd(length), 'lt': turtle.lt(args), 'bk': turtle.bk(length), 'rt': turtle.rt(args), 'nop':None}

for command in cmd:
  if command in map.keys():
      map[command]()

execute(bob, 50, 45, 'fdltfd' )
execute(bob, 50, 45, 'fdltfd')
所以去掉星号。参数
turtle
也是包的名称,因此请更改它,例如
my_turtle
。类似地,
map
是内置Python的名称,因此请更改它

def execute(turtle, length, args, *cmd):

map = {'fd': turtle.fd(length), 'lt': turtle.lt(args), 'bk': turtle.bk(length), 'rt': turtle.rt(args), 'nop':None}

for command in cmd:
  if command in map.keys():
      map[command]()

execute(bob, 50, 45, 'fdltfd' )
字典应该包含要调用的函数,而不是调用函数的结果。即代替:

def execute(turtle, length, args, *cmd):

map = {'fd': turtle.fd(length), 'lt': turtle.lt(args), 'bk': turtle.bk(length), 'rt': turtle.rt(args), 'nop':None}

for command in cmd:
  if command in map.keys():
      map[command]()

execute(bob, 50, 45, 'fdltfd' )
map = {'fd': turtle.fd(length), 'lt': turtle.lt(args), 'bk': turtle.bk(length), 'rt': turtle.rt(args), 'nop':None}
我希望有更多类似于:

def execute(turtle, length, args, *cmd):

map = {'fd': turtle.fd(length), 'lt': turtle.lt(args), 'bk': turtle.bk(length), 'rt': turtle.rt(args), 'nop':None}

for command in cmd:
  if command in map.keys():
      map[command]()

execute(bob, 50, 45, 'fdltfd' )
commands = {'fd': turtle.fd, 'lt': turtle.lt, 'bk': turtle.bk, 'rt': turtle.rt, 'nop': None}
# 'fdltfd' -> ['fd', 'lt', 'fd']
for command in [a + b for a, b in zip(cmd[0::2], cmd[1::2])]:
或:

def execute(turtle, length, args, *cmd):

map = {'fd': turtle.fd(length), 'lt': turtle.lt(args), 'bk': turtle.bk(length), 'rt': turtle.rt(args), 'nop':None}

for command in cmd:
  if command in map.keys():
      map[command]()

execute(bob, 50, 45, 'fdltfd' )
鉴于
cmd
'fdltfd'
的值,我看不出您希望它如何工作:

def execute(turtle, length, args, *cmd):

map = {'fd': turtle.fd(length), 'lt': turtle.lt(args), 'bk': turtle.bk(length), 'rt': turtle.rt(args), 'nop':None}

for command in cmd:
  if command in map.keys():
      map[command]()

execute(bob, 50, 45, 'fdltfd' )
for command in cmd:
因为它会在字典中查找“f”、“d”、“l”、“t”等,而不是“fd”、“lt”等。您可能需要类似以下内容:

def execute(turtle, length, args, *cmd):

map = {'fd': turtle.fd(length), 'lt': turtle.lt(args), 'bk': turtle.bk(length), 'rt': turtle.rt(args), 'nop':None}

for command in cmd:
  if command in map.keys():
      map[command]()

execute(bob, 50, 45, 'fdltfd' )
commands = {'fd': turtle.fd, 'lt': turtle.lt, 'bk': turtle.bk, 'rt': turtle.rt, 'nop': None}
# 'fdltfd' -> ['fd', 'lt', 'fd']
for command in [a + b for a, b in zip(cmd[0::2], cmd[1::2])]:
综上所述,我们得到了一个大致可行的实现:

def execute(turtle, length, args, *cmd):

map = {'fd': turtle.fd(length), 'lt': turtle.lt(args), 'bk': turtle.bk(length), 'rt': turtle.rt(args), 'nop':None}

for command in cmd:
  if command in map.keys():
      map[command]()

execute(bob, 50, 45, 'fdltfd' )
import turtle

LENGTH = 50
ANGLE = 45

commands = { \
    'fd': lambda t: t.fd(LENGTH), \
    'lt': lambda t: t.lt(ANGLE), \
    'bk': lambda t: t.bk(LENGTH), \
    'rt': lambda t: t.rt(ANGLE), \
    }

def execute(my_turtle, cmd):
    for command in [a + b for a, b in zip(cmd[0::2], cmd[1::2])]:
        if command in commands:
            commands[command](my_turtle)

execute(turtle.Turtle(), 'fdltfdltfdltfdltfdltfdltfdltfd')

turtle.mainloop()

非常感谢,先生。你是救命恩人。在您的帮助下,我成功地更改了execute函数,因此现在它也将长度和角度作为参数。我也忘了lambda。所以,我的海龟完全是基于函数的。再次感谢大家。