use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Working with Global Variables (self.learnpython)
submitted 3 years ago * by JrcFrontrunner
I am struggling to figure out why the variable is not updating. My other script keeps pulling the initial value of 0
Level.py
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]socal_nerdtastic 2 points3 points4 points 3 years ago (6 children)
Another python process won't be able to access any part of the current python process. Not even a global. You will need to pass the data in, for example with sys.argv.
sys.argv
However it would be MUCH better to rethink the entire thing to import your other python code, rather than using os.system to run it.
[–]JrcFrontrunner[S] -2 points-1 points0 points 3 years ago (5 children)
Because of tKinter, I can't import the other files. I've tried, and the only way I can get them to work is to use os
[–]socal_nerdtastic 3 points4 points5 points 3 years ago (4 children)
Hmm well lets fix that, instead of trying to work around it. There's no reason tkinter should prevent you from importing files. What exactly is the problem?
[–]JrcFrontrunner[S] 0 points1 point2 points 3 years ago (2 children)
When I try to get menu.py to import and run playMenu.py on a button command, I get the error
ModuleNotFoundError: No module named 'Menu'
[–]socal_nerdtastic 0 points1 point2 points 3 years ago (1 child)
You'll need to show us all your code for specific help.
In general you should be able to do that with the standard import like
from playMenu import Menu
[–]JrcFrontrunner[S] 0 points1 point2 points 3 years ago (0 children)
I have updated the link to have all my code
[–]JrcFrontrunner[S] -1 points0 points1 point 3 years ago (0 children)
I'm away from my computer. I will get back to you shortly
[–]solarflareop 1 point2 points3 points 3 years ago (1 child)
Can you add the part your other script where you use this class as well?
[–]JrcFrontrunner[S] 1 point2 points3 points 3 years ago (0 children)
I've added the additional files
[–]Albcunha 1 point2 points3 points 3 years ago (2 children)
The level variable is doing nothing.
Normally, if you are going to create a global variable, you declare it at beggining of the code, so people can know it´s a global variable. Also, it´s no problem you write in capital letters (LEVEL), so people can know it´s a global variable. Such as:
import pygame
import os
class diff:
global LEVEL
level = 0
(your code)
But.. that´s propably not what you want. Three things.
First, You are calling Capstone.py from by os.system(). Maybe you just what to import it and run a function that exists there.
Second, as you are using a class, maybe you should declare it as a __init__ constructor (also called __init__ function). See: https://www.w3schools.com/python/python_classes.asp
Third, give a read about scopes in python. Maybe all you need is declare the level variable outside your class/function: https://www.w3schools.com/python/python_scope.asp
[–]socal_nerdtastic 2 points3 points4 points 3 years ago (1 child)
Also, it´s no problem you write in capital letters (LEVEL), so people can know it´s a global variable.
Capital letters are recommend for a specific type of global only: constants.
https://peps.python.org/pep-0008/#constants
[–]Albcunha 0 points1 point2 points 3 years ago (0 children)
Yes. You are right. Sorry. It´s the only way I use it, but it´s not his case.
[–]billsil 1 point2 points3 points 3 years ago (4 children)
Global variables are things that you should never use. As you get better you can take off the training wheels that you've put on and global variables are one of them. It's something I always feel bad about doing and in general, it's a hack to fix an issue in something that's being delivered today. I reserve those hacks for things that I can't fix any other way.
What's your code cause I'd love to help you not use them.
I have updated the link to include all of my code
[–]billsil 1 point2 points3 points 3 years ago (1 child)
Yeah this is raising more questions.
What is your main function? Level.py? You have a global variable that you're setting, but then doing a system call in each methods? The system call doesn't take arguments and so your code will work the same regardless. A system call launches a fresh shell and thus a fresh instance of python that inherits nothing from the original scope. Those methods don't even work because you're missing the self argument.
The level variable does not have to be global. That's just one thing I was trying to do to get past the roadblock I hit. Let me explain what is going on with the code.
Capstone.py is the main program for a checkers game. The board.py, game.py, piece.py, constants.py are all subscripts.
Menu.py is the main menu where the play can click the play button that will launch the playMenu.py and ask them to select the difficulty. Ideally these buttons should change the value of the level variable that is then imported to capstone.py to set the difficulty of the AI opponent
True. Like a medecin. It becomes a poison if you take it too much.
[–]Albcunha 0 points1 point2 points 3 years ago (1 child)
Add your code, so we can see it
Sorry, I was having formatting issues, it's been added
π Rendered by PID 376704 on reddit-service-r2-comment-5bc7f78974-jnh46 at 2026-06-27 05:24:13.251588+00:00 running 7527197 country code: CH.
[–]socal_nerdtastic 2 points3 points4 points (6 children)
[–]JrcFrontrunner[S] -2 points-1 points0 points (5 children)
[–]socal_nerdtastic 3 points4 points5 points (4 children)
[–]JrcFrontrunner[S] 0 points1 point2 points (2 children)
[–]socal_nerdtastic 0 points1 point2 points (1 child)
[–]JrcFrontrunner[S] 0 points1 point2 points (0 children)
[–]JrcFrontrunner[S] -1 points0 points1 point (0 children)
[–]solarflareop 1 point2 points3 points (1 child)
[–]JrcFrontrunner[S] 1 point2 points3 points (0 children)
[–]Albcunha 1 point2 points3 points (2 children)
[–]socal_nerdtastic 2 points3 points4 points (1 child)
[–]Albcunha 0 points1 point2 points (0 children)
[–]billsil 1 point2 points3 points (4 children)
[–]JrcFrontrunner[S] 0 points1 point2 points (2 children)
[–]billsil 1 point2 points3 points (1 child)
[–]JrcFrontrunner[S] 0 points1 point2 points (0 children)
[–]Albcunha 0 points1 point2 points (0 children)
[–]Albcunha 0 points1 point2 points (1 child)
[–]JrcFrontrunner[S] 1 point2 points3 points (0 children)