第二次执行时,Python龟菜单循环失败

时间:2017-12-27 08:17:46

标签: python turtle-graphics

flag = 0
while(flag == 0):
    import turtle
    import math
    def reflection(x1,y1,x2,y2):
        y=[[x1,y1],[x2,y2]]
        a= 90
        m=math.radians(a)
        val= [[round(math.cos(m)),round(math.sin(m))],[round(math.sin(-m)),round(math.cos(m))]]
        re_result=[[0,0],[0,0]]
        for i in range(len(y)):
            for j in range(len(val[0])):
                for k in range(len(val)):
                    re_result[i][j] += y[i][k]*val[k][j]
        for j in re_result:
            print (j)
        import turtle
        turtle.goto(val)
        turtle.ht()
        turtle.done()

    def scale(x1,y1,x2,y2):
        dx=(x2-x1)
        float(dx)
        dy=(y2-y1)
        float(dy)
        if(abs(dx)>=abs(dy)):
            l=abs(dx)
            float(l)
        else:
            l=abs(dy)
            float(l)
        X=dx/l
        float(X)
        Y=dy/l
        float(Y)
        Xnew=x1+0.5
        float(Xnew)
        Ynew=y1+0.5
        float(Ynew)
        for i in range(int(l)):
            Xnew=Xnew+X
            float(Xnew)
            Ynew=Ynew+Y
            float(Ynew)

        import turtle
        turtle.setposition(int(Xnew),int(Ynew))
        turtle.ht()
        turtle.done()
    def trans(x1,y1,x2,y2):
        x=[[x1,y1,1],[x2,y2,1]]
        m=input("enter the value of m")
        n=input("enter the value of n")
        r=  [[1,0,0],[0,1,0],[m,n,1]]
        result=[[0,0],[0,0]]
        for i in range(len(x)):
            for j in range(len(r[0])-1):
                for k in range(len(r)):
                    result[i][j] += x[i][k]*r[k][j]
        for r in result:
            print (r)
        import turtle
        turtle.setposition(r)
        turtle.ht()
        turtle.done()
    def rotation(x1,y1,x2,y2):
        x=[[x1,y1],[x2,y2]]
        a=input("enter the angle")
        b=math.radians(a)
        r= [[round(math.cos(b)),round(math.sin(b))],
        [round(math.sin(-b)),round(math.cos(b))]]
        print "rrrr", r
        result=[[0,0],[0,0]]
        for i in range(len(x)):
            for j in range(len(r[0])):
                for k in range(len(r)):
                    result[i][j] += x[i][k]*r[k][j]
        for r in result:
            print (r)
        import turtle
        #print "turtual", turtle.goto(r)
        turtle.goto(r)
        turtle.ht()
        turtle.done()

    def draw(x1,y1,x2,y2):
        dx=(x2-x1)
        float(dx)
        dy=(y2-y1)
        float(dy)
        if(abs(dx)>=abs(dy)):
            l=abs(dx)
            float(l)
        else:
            l=abs(dy)
            float(l)
        X=dx/l
        float(X)
        Y=dy/l
        float(Y)
        Xnew=x1+0.5
        float(Xnew)
        Ynew=y1+0.5
        float(Ynew)
        for i in range(int(l)):
            Xnew=Xnew+X
            float(Xnew)
            Ynew=Ynew+Y
            float(Ynew)

        import turtle
        turtle.setposition(int(Xnew),int(Ynew))
        turtle.ht()
        turtle.done()

    x1=float(input("enter the x1 value"))
    y1=float(input("enter the y1 value"))
    x2=float(input("enter the x2 value"))
    y2=float(input("enter the y2 value"))
    flag = 0
    while(flag == 0):
        print turtle.Terminator
        print("\nMenu\n(1)draw\n(2)rotation\n(3)translation\n(4)scale\n(5)reflection\n(6)Exit")
        ch=raw_input(">>> ")
        if(ch == '1'):
            draw(x1,y1,x2,y2)
        elif(ch == '2'):
            print "xxx", x1, y1, x2, y2
            rotation(x1,y1,x2,y2)
        elif(ch == '3'):
            trans(x1,y1,x2,y2)
        elif(ch == '4'):
            dist = input("enter the scaling factor")
            scale(x1,y1,dist,dist)
        elif(ch == '5'):
            reflection(x1,y1,x2,y2)
        elif(ch == '6'):
            flag = 10
        else:
            print "enter a valid choice"
    #dist = input("enter the scaling factor")
    #draw(x1,y1,x2,y2)
    #rotation(x1,y1,x2,y2)
    #trans(x1,y1,x2,y2)
    #scale(x1,y1,dist,dist)
    #reflection(x1,y1,x2,y2)

1 个答案:

答案 0 :(得分:0)

这是您的程序大纲,应该允许您多次从菜单中进行选择 - 我已用pass替换了绘图函数的主体,因为这只是一个大纲该计划:

import turtle
import math

def reflection(x1, y1, x2, y2):
    pass

def scale(x1, y1, x2, y2):
    pass

def trans(x1, y1, x2, y2):
    pass

def rotation(x1, y1, x2, y2):
    pass

def draw(x1, y1, x2, y2):
    pass

x1 = float(input("enter the x1 value: "))
y1 = float(input("enter the y1 value: "))
x2 = float(input("enter the x2 value: "))
y2 = float(input("enter the y2 value: "))

while True:
    print("\nMenu\n(1)draw\n(2)rotation\n(3)translation\n(4)scale\n(5)reflection\n(6)Exit")
    ch = input(">>> ")

    if ch == '1':
        draw(x1, y1, x2, y2)
    elif ch == '2':
        rotation(x1, y1, x2, y2)
    elif ch == '3':
        trans(x1, y1, x2, y2)
    elif ch == '4':
        dist = float(input("enter the scaling factor: "))
        scale(x1, y1, dist, dist)
    elif ch == '5':
        reflection(x1, y1, x2, y2)
    elif ch == '6':
        turtle.bye()
        exit(0)
    else:
        print("enter a valid choice")

turtle.done()

只需拨打turtle.done()一个电话,作为您节目的最后一行 - 摆脱其他人,因为他们正在对您不利。在整个代码中,这没有任何意义:

Y=dy/l
float(Y)

因为float()返回一个未使用的值,因此是无操作。 (即它没有修改它的论点。)你可能想要的是:

Y = float(dy // l)

这样的代码:

a=input("enter the angle")
b=math.radians(a)
由于inputstr并且您需要float

将无法运作

a = float(input("enter the angle (in degrees): "))
b = math.radians(a)

您只需在代码顶部import turtle一次。