问题描述:

I am using Windows 10, PyScripter 2.6, Python 3.4.4.

I'm new to programing. I am learning python and creating programs with the turtle module.

The turtle module creates a window and has turtles draw what you tell them to draw on the canvas of the window.

When there is an error in my turtle program, the turtle window will go into a "not responding" state. It crashes. I have to close it, it sends an error report to Microsoft, etc. etc.

My question is, is there some code or a some way to prevent the "turtle window" from crashing and going into a "not responding" state?

I've tried debug and syntax check, but they do not prevent the problem.

Part of me tells me this is just the way it is. If you write bad code, your programs will crash, but it just seems like, in the development environment, there would be a way to "deal" with these things.

Thanks

Tim

Code and Error Messages

CODE

def main():

pass

if __name__ == '__main__':

main()

import turtle

wn = turtle.Screen()

wn.bgcolor("lightgreen")

wn.title("Tess & Alex")

tess = turtle.Turtle()

tess.color("hotpink")

tess.pensize(5)

alex = turtle.Turtle

tess.forward(80)

tess.left(120)

tess.forward(80)

tess.left(120)

tess.forward(80)

tess.left(120)

tess.right(180)

tess.forward(80)

for x in [0,1,2,3]:

alex.forward(50)

alex.left(90)

wn.mainloop()

ERROR

Message File Name Line Position

Traceback

C:\py\3\program1.py 41

TypeError: forward() missing 1 required positional argument: 'distance'

网友答案:

The bug in your code is this line:

alex = turtle.Turtle

which should be:

alex = turtle.Turtle()

But let's address your larger question. I assume that simple bugs in your program are causing long, drawn out crashes making it hard to do quick turn around debug and test cycles. (If not, just live with the current sitation.) Although I can't reproduce the behaviour on my system, there's something we can try (for development only, as folks will frown upon if it survives into your finished code as the exception is too broad):

import sys
from turtle import Turtle, Screen

try:
    screen = Screen()
    screen.bgcolor("lightgreen")
    screen.title("Tess & Alex")

    tess = Turtle()
    tess.color("hotpink")
    tess.pensize(5)

    for _ in range(3):
        tess.forward(80)
        tess.left(120)

    tess.right(180)
    tess.forward(80)

    alex = Turtle

    for _ in range(4):
        alex.forward(50)
        alex.left(90)

except Exception as e:
    exc_type, exc_obj, exc_tb = sys.exc_info()
    print(exc_type, "Line:", exc_tb.tb_lineno, "\n", e)
    exit()

screen.mainloop()

Note that I've intentionally kept your missing ()'s error. When it hits the error this time, within the try and except, it should hopefully return immediately with:

<class 'TypeError'> Line: 23 
 forward() missing 1 required positional argument: 'distance'

rather going into a "not responding" state on you. Give it a try, let us know if it helps.

相关阅读:
Top