如何在函数之间传递变量

时间:2013-09-23 02:43:34

标签: python turtle-graphics

所以这个程序让我可以制作一个可定制的棒图。我是python的新手,这是我在学校计算机科学之外的第一个大项目。由于身体臂的坐标在不同的地方结束,我需要bodyfunc的坐标,短,中,长。这个程序有效,但由于某种原因,它没有获得价值并将其传递给我的尾部/配件功能。你能看看我的节目并帮助我!

import turtle


#Head
def headfunc(headtype, headcolor, headpensize):
    head=turtle.Turtle()
    head.color(headcolor)
    head.pensize(headpensize)
    if headtype=="square":
        wn = turtle.Screen()
        head.penup()
        head.left(90)
        head.forward(200)
        head.pendown()
        head.left(90)
        head.forward(50)
        head.right(90)
        head.forward(100)
        head.right(90)
        head.forward(100)
        head.right(90)
        head.forward(100)
        head.right(90)
        head.forward(50)

    elif headtype=="round":
        wn = turtle.Screen()
        head.penup()
        head.left(90)
        head.forward(200)
        head.right(90)
        head.pendown()
        head.circle(50)

    elif headtype=="diamond":
        wn = turtle.Screen()
        head.penup()
        head.goto(0,200)
        head.pendown()
        head.goto(35,300)
        head.goto(0,400)
        head.goto(-35,300)
        head.goto(0,200)

    else:
        print("Not valid")
# Body
def bodyfunc(bodytype,bodycolor, bodypensize, endbody, endarm_x, endarm_y):
    body=turtle.Turtle()
    body.color(bodycolor)
    body.pensize(bodypensize)
    if bodytype=="short":
        bodylength = 200
        wn = turtle.Screen()
        body.penup()
        body.goto(0,200)
        body.pendown()
        body.goto(0,200-bodylength)  #Body Length
        body.goto(0,200-3*bodylength/8)                       #Body(Reposition)
        body.goto(bodylength/2,200-bodylength/4)  #Arm Length (Right)
        body.goto(0,200-3*bodylength/8)                       #Body (Reposition)
        body.goto(-bodylength/2,200-bodylength/4) #Arm Length (Left)
        body.goto(0,200-3*bodylength/8)                       #Body (Reposition)
        body.goto(0,200-bodylength)                      #Body (Reposition)
        body.goto(bodylength/4, 200-3*bodylength/2) #Leg Length (Right)
        body.goto(0,200-bodylength)                      #Body (Reposition)
        body.goto(-bodylength/4,200-3*bodylength/2)#Leg Length(Left)
        endbody = 200-bodylength
        endarm_x = bodylength/2
        endarm_y = 200-bodylength/4
        return endbody, endarm_x, endarm_y

    elif bodytype=="medium":
        bodylength = 300
        wn = turtle.Screen()
        body.penup()
        body.goto(0,200)
        body.pendown()
        body.goto(0,200-bodylength)  #Body Length
        body.goto(0,200-3*bodylength/8)                       #Body(Reposition)
        body.goto(bodylength/2,200-bodylength/4)  #Arm Length (Right)
        body.goto(0,200-3*bodylength/8)                       #Body (Reposition)
        body.goto(-bodylength/2,200-bodylength/4) #Arm Length (Left)
        body.goto(0,200-3*bodylength/8)                       #Body (Reposition)
        body.goto(0,200-bodylength)                      #Body (Reposition)
        body.goto(bodylength/4,200-3*bodylength/2) #Leg Length (Right)
        body.goto(0,200-bodylength)                      #Body (Reposition)
        body.goto(-bodylength/4,200-3*bodylength/2)#Leg Length(Left)
        endbody = 200-bodylength
        endarm_x = bodylength/2
        endarm_y = 200-bodylength/4
        return endbody, endarm_x, endarm_y

    elif bodytype=="long":
        bodylength = 400
        wn = turtle.Screen()
        body.penup()
        body.goto(0,200)
        body.pendown()
        body.goto(0,200-bodylength)  #Body Length
        body.goto(0,200-3*bodylength/8)                       #Body(Reposition)
        body.goto(bodylength/2,200-bodylength/4)  #Arm Length (Right)
        body.goto(0,200-3*bodylength/8)                       #Body (Reposition)
        body.goto(-bodylength/2,200-bodylength/4) #Arm Length (Left)
        body.goto(0,200-3*bodylength/8)                       #Body (Reposition)
        body.goto(0,200-bodylength)                      #Body (Reposition)
        body.goto(bodylength/4,200-3*bodylength/2) #Leg Length (Right)
        body.goto(0,200-bodylength)                      #Body (Reposition)
        body.goto(-bodylength/4,200-3*bodylength/2)#Leg Length(Left)
        endbody = 200-bodylength
        endarm_x = bodylength/2
        endarm_y = 200-bodylength/4
        return endbody

    else:
        print("Not valid")
# Tail
def tailfunc(tail, endbody):
    if tail=="yes":
        wn = turtle.Screen()
        tail=turtle.Turtle()
        tail.penup()
        print(endbody)
        tail.goto(0,endbody)
        tail.pendown()
        tail.color("brown")
        tail.right(65)
        tail.circle(60,200)
        tail.shape("triangle")

    elif tail=="no":
        quiterty=1

    else:
        print("Not valid")

def accessories1func(accessories1, endarm_x, endarm_y):
    a=turtle.Turtle()
    if accessories1=="sword":
        wn = turtle.Screen()
        a.goto(endarm_x, endarm_y)
        a.forward(100)
        wn.mainloop()


def main():
    headtype=input("Enter head(square,round,diamond)")
    headcolor=input("Head Color?:Red,white,black,gray,brown,gold,purple,blue,cyan")
    headpensize=input("Head pensize? 1-5")
    bodytype=input("Enter body(short,medium,long")
    bodycolor=input("Body Color?:Red,white,black,gray,brown,gold,purple,blue,cyan")
    bodypensize=input("Head pensize? 1-5")
    tail=input("Tail: Yes or no")
    accessories1=input("sword,staff,gun,bow")

    endbody = 0
    endarm_x = 0
    endarm_y = 0

    headfunc(headtype,headcolor, headpensize)
    bodyfunc(bodytype,bodycolor, bodypensize, endbody, endarm_x, endarm_y)
    tailfunc(tail,endbody)
    accessories1func(accessories1, endarm_x, endarm_y)

main()

2 个答案:

答案 0 :(得分:1)

您不捕获返回值。您需要执行以下操作:

endbody, endarm_x, endarm_y = bodyfunc(bodytype,bodycolor, bodypensize, endbody, endarm_x, endarm_y)

但看起来bodyfunc的返回类型不一致,因此这可能会给您带来其他问题。

答案 1 :(得分:1)

我认为通过创建课程可以获得最佳效果。

class stickfigure(object):
  def __init__(self):
    self.headtype=input("Enter head(square,round,diamond)")
    self.headcolor=input("Head Color?:Red,white,black,gray,brown,gold,purple,blue,cyan")
    # you get the idea.

这样,您要保留的值就是您创建的每个棒图的成员。