#πŸ”’ Beginner: Reset global scope counter used in a function

32 messages Β· Page 1 of 1 (latest)

fossil frigate
#
##
## 1.) defining function
##        
def SecondHalfMoveAlgo():
    xx = get_pos_x()
    yy = get_pos_y()
    if xx == 2 and yy < 3:
        move(North)
    elif xx == 2 and yy == 3:
        move(North)
        move(East)
    elif xx == 3 and yy < 3:
        move(North)
    elif xx == 3 and yy == 3 and count < 2:
        move(North)
        move(West)
    elif xx == 3 and yy == 3 and count == 2:
        move(East)
        move(North)
        count = 0
        quick_print(count, "Reset")
##
##
## 2.)execution part of code
#
clear()
count = 0
initialPlanting(3)
while True:
    if get_pos_x() <= 1:
        harvestCarrBushSun()
        HalfMapAlgoMove()
    elif get_pos_x() >= 2:
            harvestCarrBushSun()
            SecondHalfMoveAlgo()
            if get_pos_x() == 3 and get_pos_y() == 3:
                count +=1
                quick_print(count)    ```


I have this code. In the `execution part` is the golbal variable `count = 0` defined. The counter will be `+=1` after the function `SecondHalfMoveAlgo` is executed. In the same function in the last elif satement I try to reset the count to zero again. The following error occurs
`Error: Trying to create a local variable with the name count. count already exists in an outer scope.` I understand the Error. But I dont understand how to fix this or work around this.
strong cliffBOT
#

@fossil frigate

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

quaint trail
lost folio
fossil frigate
#

Thanks for the fast help πŸ–€ lets assume I do not have the global function. Is there a way?

quaint trail
#

If you're not allowed to use it for some reason, no. You would need to do something like return the new count.

fossil frigate
#

I am not allowed to πŸ˜…

quaint trail
#

Then no.

lost folio
#

then you need to pass the information as a parameter to the function and return the new value from the function

quaint trail
#

You'd need to return the new score and assign the global at the callsite. Or do something hacky like store the global in a mutable container, and then mutate the container.

#

(Prefer the former. The latter was just an example of how this could also be done in a pinch)

lost folio
#

hahaha, yeah, that is hacky
something like a list or dict
not preferred, but certainly creative πŸ˜†

quaint trail
#

It's kind of a step down from classes, so it's worth at least understanding, I think. Once you make a class, the self would be the mutable object holding what may otherwise be globals.

#

But ya, using a list purely to avoid global would not be a good idea for real code, but it's worth looking into and understanding why it works.

fossil frigate
#

let me understand what you are sayingπŸ˜…
instead of count = 0 put this into a list ( only object in there). than, while defining the function, make a change to the index in the list? Than later get the changed index in the list and use this as the counter?

#

somehow... maybe... I dont understand myself ^^

quaint trail
#

!e

some_global = [0]

def func(x):
    x[0] = 1

func(some_global)
print(some_global)
strong cliffBOT
quaint trail
#

But don't actually do this in real code if the goal is just to avoid global

lost folio
#

variables only hold a reference to mutable objects in python, so you can modify their content from anywhere where you can access that reference

#

when you do variable_name = (and then some more code) to a variable that already exist, you are pointing that variable to new data, replacing the old reference to the prior data

fossil frigate
quaint trail
fossil frigate
fossil frigate
fossil frigate
#

works like a charm now

#

thanks guys! much much appreciated!

lost folio
quaint trail
#

(Please don't say the latter. I'm beginning to regret mentioning it)

strong cliffBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.