脚本在Python中运行,但在Python shell

时间:2017-12-13 18:44:17

标签: python turtle-graphics python-idle

我是C ++的真正新手。我教高中技术应用课程,但我的校长在年中给了我一个编程课程。我在课堂上领先一步我做得很好,但我有一个程序由一个不起作用的学生上交,我看不出有什么问题。如果我将它直接粘贴到Python命令行中,它可以正常工作。如果我在IDLE中打开它不起作用。 这是脚本:

""" For this program to work as planned
use the keys W,A,D, and SPACE
W will draw a circle
A will draw a triangle
D will end the program
and Space will move the turtle """


from turtle import *


#this draws you a red circle
def draw_circle():
    # This function draws a red circle
    color("red")
    width(10)
    penup()
    goto(-200, -200)
    pendown()
    circle(20)



#this draws a green trinangle

def draw_triangle():
    # This function draws a green triangle 
    color("green")
    width(10)
    penup()
    goto(0, -200)
    pendown()
    right(180)
    circle(15, steps =3)

#this moves the turtle

def move_turt():
    penup()
    goto(-200, 100)
    pendown
    #this ends the program

def end():
    bye()


draw_circle()
draw_triangle()

listen()

onkey(move_turt, "space")
onkey(draw_circle, "w")
onkey(draw_triangle, "a")
onkey(end, "d")

1 个答案:

答案 0 :(得分:1)

  

如果我将它直接粘贴到python命令行中,它可以正常工作。   如果我在IDLE中打开它就不起作用。

这很奇怪,当我在python3下的命令行运行时,我看到了相反的情况,由于缺少了最后致电mainloop()。但它在Python的idle3下工作正常,因为该环境不需要调用mainloop()

然而,粘贴到Python解释器中确实有效,因为它没有终止程序。

我对代码的修改,以及它的价值:

"""
For this program to work as planned
use the keys W, A, D, and SPACE
W will draw a circle
A will draw a triangle
D will end the program
and Space will move the turtle
"""

from turtle import *

def draw_circle():
    """ This function draws a red circle """
    color("red")
    width(10)
    penup()
    goto(-200, -200)
    pendown()
    circle(20)

def draw_triangle():
    """ This function draws a green triangle """
    color("green")
    width(10)
    penup()
    goto(0, -200)
    pendown()
    right(180)
    circle(15, steps=3)

def move_turt():
    """ move the turtle """
    penup()
    goto(-200, 100)
    pendown()

def end():
    """ end the program """
    bye()

onkey(move_turt, "space")
onkey(draw_circle, "w")
onkey(draw_triangle, "a")
onkey(end, "d")

listen()
mainloop()

您使用的是Python 3还是Python 2?