Two problems here:
elif items_in_line[0] == ("back" or "backward"):
This means "backward"
will never work. Try typing this in an interactive window:
>>> ("back" or "backward")
'back'
So, checking if something is equal to ("back" or "backward")
is the same as checking if it's equal to "back"
.
You want this:
elif items_in_line[0] in ("back", "backward"):
or, if you insist, this:
elif items_in_line[0] == "back" or items_in_line[0] == "backward":
Then, there's the other problem:
if isinstance(m,int) == True:
turtle.forward(m)
Since items_in_line
is the result of a split
call, each element has to be a string, so it can't possibly be an int
. (Also, you shouldn't do == True
in Python, except when you specifically want to distinguish True
from other true values.)
What you may want is something like this:
try:
amount_to_move = int(m)
except ValueError as e:
print(<some message about the error>)
else:
turtle.forward(amount_to_move)
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:
def comm(items_in_line):
try:
m = int(items_in_line[1])
except ValueError:
print(<some message>)
return
if items_in_line[0] == "forward":
turtle.forward(m)
if items_in_line[0] in ("backward", "back"):
turtle.back(m)
Or maybe you don't even need the try
/except
here, because you can handle it at a higher level—after all, if items_in_line
only has 1 element, it's going to raise an IndexError
that you're not catching, so why not treat "forward foo"
the same way you treat "forward"
and let it trickle up to the caller?
def comm(items_in_line):
m = int(items_in_line[1])
if items_in_line[0] == "forward":
turtle.forward(m)
if items_in_line[0] in("backward", "back"):
turtle.back(m)