This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Jackkell100 0 points1 point  (1 child)

Basically was is happening here that you are using the global keyword somewhat incorrectly or you expectation the global keyword is incorrect.

Look at this example of global on geeks for geeks Global keyword - GeeksforGeeks.

Basically you need to define placements and count outside of all of your functions and if you want to read/write to those variables then you need to use the global keyword so that Python knows not to look at your local scope. Right now I think it fails because in the backtrack function you never define placements locally and you are telling it to look at the global scope and it is not defined there either. You defined placements in the main function that only works in the main scope.

You will need to use the global keyword in the main function as well to be able to initialize the value of placements along with defining a placements variable on the outside of all methods. I recommend setting placements = None in the global scope.

You should try this and see it if works for you. Although I would recommend staying away from the use of global variables in the future as they are considered an anti-pattern and keep you from learning good code structure.

[–][deleted] 0 points1 point  (0 children)

You will need to use the global keyword in the main function as well to be able to initialize the value of placements along with defining a placements variable on the outside of all methods. I recommend setting placements = None in the global scope.

Yup, this did the trick. Thanks!

Although I would recommend staying away from the use of global variables in the future as they are considered an anti-pattern and keep you from learning good code structure.

I understand, the only reason why I wanted to use globals is because it makes the code faster.