Python参数未定义

时间:2013-09-05 17:41:36

标签: python syntax parameters

所以我正在制作一个使用乌龟绘制特定字母的程序,我已经设置了一个参数比例,以根据输入的提示来影响程序的大小和笔厚度。这就是我所拥有的...我一直收到一个错误,称未定义比例。

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)

1 个答案:

答案 0 :(得分:1)

main功能中,您有代码

scale == input("Enter scale:")

但这是比较而不是分配,因此您收到错误,因为未定义scale。要为变量scale赋值(并因此定义它),您必须使用单=
要解决这个问题:

scale = input("Enter scale:")

另一件事是,您的drawLetter(letter)无法访问scale变量,因此您必须将其添加为参数。
要解决这个问题:

def drawLetter(letter, scale):

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