0
import turtle
import random

wn = turtle.Screen() #sets the screen
wn.setup(1000,900)
wn.screensize(2000,2000)
ad = turtle.Turtle() #names the turtle
ad.shape("circle") #changes turtles or "ad's" shape
ad.speed("fastest")

r = int(60) #CHANGES THE SIZE OF THE WRITING


x = int(-950)
y = int(200)


ad.penup()
ad.goto(x,y)
def enter():
     ad.penup()
     y -= 100
     ad.goto(x,y)
wn.onkey(lambda: enter(), "Return")
wn.listen()

Trying to do an enter button in turtle but this wont work. It says that there is an error with the local variable.

4
  • show your error Commented Jun 6, 2018 at 19:48
  • x and y have a scope problem. Commented Jun 6, 2018 at 19:49
  • UnboundLocalError: local variable 'y' referenced before assignment Commented Jun 6, 2018 at 19:50
  • how do I correct Commented Jun 6, 2018 at 19:50

1 Answer 1

1

Although your immediate problem is the lack of a global y statement in your enter() function, there's a lot of noise in your code that we should eliminate to make it a better MVCE:

import random  # not used so leave out of SO question example
r = int(60) # ditto

x = int(-950)  # int() not needed for ints
y = int(200)  # ditto

wn.onkey(lambda: enter(), "Return")  # lambda not needed

Although we could fix this with the addition of a global y statement, I prefer to simply interrogate the turtle itself and avoid the global:

from turtle import Turtle, Screen

def enter():
    ad.sety(ad.ycor() - 100)

X, Y = -950, 200

wn = Screen()
wn.setup(1000, 1000)  # visible field
wn.screensize(2000, 2000)  # total field

ad = Turtle("circle")
ad.speed("fastest")
ad.penup()
ad.goto(X, Y)

wn.onkey(enter, "Return")
wn.listen()

wn.mainloop()

Note, you positioned the turtle off the visible portion of the screen so you need to scroll to the left side of the window to see the turtle cursor. Once you do, you can move it down by hitting, "Return".

Sign up to request clarification or add additional context in comments.

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.