How can I change local variable inside my fuction

Viewed 90

I'm doing an easy program in python where I have my turtle and can go around and it draws but I want to make it so that when I press shift the .pensize(x) goes up by one and when I press control it goes down by one but I don't know how to do it.

Code:

def pensizeUp():
    x = 0
    x += 1
    k.pensize(x)
def pensizeDown():
    x = 0
    x -= 1
    k.pensize(x) 

wn.onkeypress(pensizeUp, "Shift_L")
wn.onkeypress(pensizeDown, "Control_l")

I tried searching it up but I couldn't find out how to do it. I also tried different things but in the end I wasn't able to figure it out.

4 Answers

So your first problem is that you are doing x = 0 every time you call the functions and your x is not global

x = 0
def pensizeUp():
    global x
    x += 1
    k.pensize(x)
def pensizeDown():
    global x
    x -= 1
    k.pensize(x)

wn.onkeypress(pensizeUp, "Shift_L")
wn.onkeypress(pensizeDown, "Control_l")

You should move the x variable outside the functions and add a global x inside each:

x = 0
def pensizeUp():
    global x
    x += 1
    k.pensize(x)
def pensizeDown():
    global x
    x -= 1
    k.pensize(x) 

wn.onkeypress(pensizeUp, "Shift_L")
wn.onkeypress(pensizeDown, "Control_l")

If you don't want to use global variables, than you can create your own class and store x into it:

class myGlobal:
    x = 0

def pensizeUp():
    myGlobal.x += 1
    
def pensizeDown():
    myGlobal.x -= 1

myGlobal.x = 100  # Value at begining
print(myGlobal.x)  # Output: 100

pensizeUp()
print(myGlobal.x)  # Output: 101

pensizeDown()
print(myGlobal.x)  # Output: 100

UPD: I think, when you talk about changing the local variable inside function you want something like this:

# THIS PART OF THE CODE SHOULDN'T WORK
def pensizeUp():
    x += 1
    k.pensize(x)

def pensizeDown():
    x -= 1
    k.pensize(x)

The problem is that Python can really get values from global scope but can't change them. As solution you can also try this:

def pensizeUp():
    xx = x + 1
    k.pensize(xx)
    return xx

def pensizeDown():
    xx = x - 1
    k.pensize(xx)
    return xx

x = 100  # Value at begining
print(x)  # Output: 100

x = pensizeUp()
print(x)  # Output 101

x = pensizeDown()
print(x)  # Output 100

Here, inside functions, Python automatically find and read x from your main part of the code. But as you can see, than you need to create additional assignment to save new value and after return it. Though I recommend avoid this practice, because on big projects even getting value from outside the function without using global scope or classes can be pretty unpredicted.

The pen size is maintained as an internal variable of the object. The turtle module implements an older style of getter and setter that you can use, instead of maintaining a separate, external variable. Here is a basic example (you may have to adapt it).

import turtle


def bigger():
    t = turtle.getturtle()
    t.pensize(t.pensize() + 1)

def smaller():
    t = turtle.getturtle()
    t.pensize(t.pensize() - 1)


def setup():
    turtle.getscreen().onkeypress(bigger, "Shift_L")
    turtle.getscreen().onkeypress(smaller, "Control_L")
    turtle.listen()

setup()
turtle.mainloop()
Related