使用下面的函数,输入在空间上分割(即向前 20),turtle 将执行颜色和写入函数,但使用 forward、back、right 或 left 什么都不做,只会打开一个空白的海龟窗口
这是我的函数和代码的精简版本,用于前进和后退命令:
import sys
import turtle
def parse_line(line):
global items_in_line
items_in_line = line.split(" ",1)
if items_in_line[0] == "forward":
if isinstance(items_in_line[1], int):
return items_in_line
elif items_in_line[0] == ("back" or "backward"):
if isinstance(items_in_line[1], int):
return items_in_line
return items_in_line
def comm(items_in_line):
m = items_in_line[1]
if items_in_line[0] == "forward":
if isinstance(m,int) == True:
turtle.forward(m)
if items_in_line[0] == ("backward" or"back"):
if isinstance(m,int) == True:
turtle.back(m)
line=input("Enter a turtle command or enter 'file' to load commands from a file")
x = parse_line(line)
y=comm(items_in_line)