Question

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)
Était-ce utile?

La solution

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)

Autres conseils

The element in your list will never be an int; if you want them to be ints then you need to pass them to the int() constructor, catching any exception that occurs.

>>> '3'
'3'
>>> int('3')
3
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top