##
## 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.
#π Beginner: Reset global scope counter used in a function
32 messages Β· Page 1 of 1 (latest)
@fossil frigate
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.
Closes after a period of inactivity, or when you send !close.
If a function reassigns a global, you need a global statement in the function so the assignment is compiled as a global assignment instead of a local one.
you need to put global count somewhere inside of your SecondHalfMoveAlgo() function before the code that tries to set/modify the global variable in that function (count = 0)
usually that is done at the top of the function for those variables that the function will want to change
Thanks for the fast help π€ lets assume I do not have the global function. Is there a way?
You always have the global statement. It's a part of the language.
If you're not allowed to use it for some reason, no. You would need to do something like return the new count.
I am not allowed to π
Then no.
then you need to pass the information as a parameter to the function and return the new value from the function
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)
hahaha, yeah, that is hacky
something like a list or dict
not preferred, but certainly creative π
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.
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 ^^
!e
some_global = [0]
def func(x):
x[0] = 1
func(some_global)
print(some_global)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
[1]
But don't actually do this in real code if the goal is just to avoid global
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
I wont do that later on. Right now I would love to understand the solution. I will go and implement and try the syntax you posted
If you need this for an assignment, it would probably be cleaner to just return True/False from the function to determine if the count should be reset
Yes but the function I am defining, is not generating anything. But I need it to stop after 2 times and choose the different rout elif
there is no time limit. gladly ^^
what did you end up using in the end, a return value from the function or that hacky thing with a mutable data structure in a global variable?
(Please don't say the latter. I'm beginning to regret mentioning it)
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.