0

所以我正在制作一个程序来使用turtle绘制特定的字母,并且我已经设置了一个参数比例来根据输入提示影响程序的大小和笔粗细。这就是我所拥有的...我不断收到错误消息,说未定义比例。

import turtle

def drawLetter( letter ):
    if letter == " ":
        space(scale)
    elif letter == "i":
        i_lc(scale)
    elif letter == "I":
        i_uc(scale)
    elif letter == "s":
        s_lc(scale)
    elif letter == "S":
        s_uc(scale)
    elif letter == "C":
        c_uc(scale)
    elif letter == "c":
        c_lc(scale)


def space(scale):
    turtle.up()
    turtle.forward( 30 * scale )


def questionMark():
    """ Draw a question mark.
    """
    turtle.forward( 10 )
    turtle.down()
    turtle.left( 90 )
    turtle.forward( 2 )
    turtle.up()
    turtle.forward( 3 )
    turtle.down()
    turtle.forward( 5 )
    turtle.right( 90 )
    turtle.forward( 10 )
    turtle.left( 90 )
    turtle.forward( 10 )
    turtle.left( 90 )
    turtle.forward( 20 )
    turtle.left( 90 )
    turtle.forward( 5 )
    turtle.up()
    turtle.forward( 15 )
    turtle.left( 90 )
    turtle.forward ( 30 )


def i_uc(scale):
    """ Draw an upper case I.
    """
    turtle.down()
    turtle.forward( 10 )
    turtle.left( 90 )
    turtle.forward( 20 )
    turtle.left( 90 )
    turtle.forward( 10 )
    turtle.left( 180 )
    turtle.forward( 20 )
    turtle.left( 180 )
    turtle.forward( 10 )
    turtle.left( 90 )
    turtle.forward( 20 )
    turtle.left( 90 )
    turtle.forward( 10 )
    turtle.up()
    turtle.forward( 10 )


def i_lc(scale):
    """ Draw a lower case i.
    """
    turtle.forward( 10 )
    turtle.down()
    turtle.left( 90 )
    turtle.forward( 11 )
    turtle.up()
    turtle.forward( 2 )
    turtle.down()
    turtle.forward( 2 )
    turtle.up()
    turtle.right( 180 )
    turtle.forward( 15 )
    turtle.left( 90 )
    turtle.forward( 20 )


def s_uc(scale):
    """ Draw an upper case S.
    """
    turtle.left( 90 )
    turtle.forward( 20 )
    turtle.right( 90 )
    turtle.forward( 20 )
    turtle.right( 180 )
    turtle.down()
    turtle.forward( 20 )
    turtle.left( 90 )
    turtle.forward( 10 )
    turtle.left( 90 )
    turtle.forward( 20 )
    turtle.right( 90 )
    turtle.forward( 10 )
    turtle.right( 90 )
    turtle.forward( 20 )
    turtle.left( 180 )
    turtle.up()
    turtle.forward( 30 )


def s_lc(scale):
    """ Draw a lower case s.
    """
    turtle.down()
    turtle.forward( 20 * scale )
    turtle.left( 90 )
    turtle.forward( 5 * scale )
    turtle.left( 90 )
    turtle.forward( 20 * scale )
    turtle.right( 90 )
    turtle.forward( 5 * scale )
    turtle.right( 90 )
    turtle.forward( 20 * scale )
    turtle.up()
    turtle.right( 90 )
    turtle.forward( 10 * scale )
    turtle.left( 90 )
    turtle.forward( 10 * scale )


def c_uc(scale):
    turtle.down()
    turtle.forward( 20 * scale )
    turtle.forward( -20 * scale )
    turtle.left( 90 )
    turtle.forward( 20 * scale )
    turtle.right( 90 )
    turtle.forward( 20 * scale )
    turtle.forward( -20 * scale )
    turtle.left( 90 )
    turtle.forward( -20 * scale )
    turtle.up()
    turtle.right( 90 )
    turtle.forward( 20 * scale )


def c_lc(scale):
    turtle.down()
    turtle.forward( 20 * scale )
    turtle.forward( -20 * scale )
    turtle.left( 90 )
    turtle.forward( 10 * scale )
    turtle.right( 90 )
    turtle.forward( 20 * scale )
    turtle.forward( -20 * scale )
    turtle.left( 90 )
    turtle.forward( -10 * scale )
    turtle.up()
    turtle.right( 90 )
    turtle.forward( 20 * scale )


def main():
    scale == input("Enter scale:")
    turtle.pensize(scale)
4

1 回答 1

1

在您的main函数中,您有代码

scale == input("Enter scale:")

但这是一个比较,而不是一个分配,所以你得到一个错误,因为scale没有定义。要给变量scale一个值(并因此定义它),您必须使用一个 sing =
要解决此使用:

scale = input("Enter scale:")

另一件事是,您drawLetter(letter)将无法访问该scale变量,因此您必须将其作为参数添加到那里。
要解决此使用:

def drawLetter(letter, scale):

第三个问题是,该drawLetter函数永远不会被调用!

于 2013-09-05T18:02:49.027 回答