1

here's my code i'm using the python turtle package:

#setup
import turtle
wn = turtle.Screen()
obj = turtle.Turtle()
go = True


def restart(x, y, go = go):
    go = False
    print(go)
wn.onscreenclick(restart)
wn.listen()

#main loop
while go:
    wn.update()
    obj.forward(0.1)

print("game ended")

when i click the screen it should stop do the code after. the loop won't stop and it won't say "game ended" i am not sure why.

I need help. thanks!

1
  • Consider accepting an answer that will help others. Commented May 6, 2020 at 4:55

2 Answers 2

1

You're defining a local variable go in your restart function, when you set it to False you are only changing the local variable's value not the go variable from the outer scope

def restart(x, y, go=go):  # This keyword argument is creating a local variable

Just remove the argument and you will then modify the correct variable

def restart(x, y):
    go = False
Sign up to request clarification or add additional context in comments.

Comments

0

Along with your global variable issue, that @IainShelvington points out, I recommend you redesign your program to use turtle timer events:

from turtle import Screen, Turtle

def restart(x, y):
    global running
    running = False

def move():
    if running:
        turtle.forward(0.1)
        screen.ontimer(move)
    else:
        screen.bye()

turtle = Turtle()

screen = Screen()
screen.onscreenclick(restart)

running = True

move()

screen.mainloop()

print("game ended")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.