简体   繁体   English

乌龟图形未从功能中绘制

[英]Turtle graphics not drawing from function

using the function below, and input which is split on space (ie 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 使用下面的功能,以及在空间上分割的输入(即正向20),乌龟将执行颜色和写入功能,但是使用正向,向后,向右或向左不执行任何操作,只显示一个空白的乌龟窗口

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)

The element in your list will never be an int ; 您列表中的元素永远不会是int if you want them to be int s then you need to pass them to the int() constructor, catching any exception that occurs. 如果希望它们为int则需要将它们传递给int()构造函数,以捕获发生的任何异常。

>>> '3'
'3'
>>> int('3')
3

Two problems here: 这里有两个问题:

elif items_in_line[0] == ("back" or "backward"):

This means "backward" will never work. 这意味着"backward"永远不会起作用。 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" . 因此,检查某项是否等于("back" or "backward")与检查其是否等于"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 . 由于items_in_linesplit调用的结果,因此每个元素都必须是字符串,因此不可能是int (Also, you shouldn't do == True in Python, except when you specifically want to distinguish True from other true values.) (此外,除非您特别想将True与其他true值区分开,否则您不应该在Python中使用== True 。)

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? 或者,也许你甚至都不需要try / except这里,因为你可以在一个较高的水平,后处理这一切,如果items_in_line只有1元,这将引发一个IndexError你没有赶上,所以为什么不把"forward foo"你对待同样的方式"forward" ,让它滴入到调用者?

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM