using the function below, and input which is split on space (i.e. forward 20), turtle will perform the color and write functions but using forward, back, right or left does nothing, just brings up a blank turtle window
here’s a condensed version of my functions and code for forwards and back commands:
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)
Two problems here:
This means
"backward"will never work. Try typing this in an interactive window:So, checking if something is equal to
("back" or "backward")is the same as checking if it’s equal to"back".You want this:
or, if you insist, this:
Then, there’s the other problem:
Since
items_in_lineis the result of asplitcall, each element has to be a string, so it can’t possibly be anint. (Also, you shouldn’t do== Truein Python, except when you specifically want to distinguishTruefrom other true values.)What you may want is something like this:
The way your code is structured, where you do the same check in both branches of the function, it’s probably better to move it upward, so:
Or maybe you don’t even need the
try/excepthere, because you can handle it at a higher level—after all, ifitems_in_lineonly has 1 element, it’s going to raise anIndexErrorthat you’re not catching, so why not treat"forward foo"the same way you treat"forward"and let it trickle up to the caller?