Ask coding questions

← Back to all posts
HELP! How do I use the same variable over multiple python scripts
ThatOneDude1 (126)

In one of the games, that I coded in python, I was recently working on I had multiple scripts and I needed to use one of the variables in one script in another and I was wondering how I would do this? Would I need to use a Database? And if so, what databases does repl.it support? Please answer!!! Thanks!
-ThatOneDude1

Commentshotnewtop
Coder100 (11092)

No! Stop thinking databases!
Use databases only when you need:

  • data to save over multiple sessions

smh

it's actally quite simple,

# main.py
myvar = 5

import other
other.func(myvar)
# other.py
def func(myvar):
  print(myvar)

see, super easy right? And the thing is, you don't have to import anything, you just change how it's executed!

ThatOneDude1 (126)

Let's say that I did want the user to be able to save this variable to use over multiple sessions? I made a game a little bit ago that is suppose to be a text based platformer. I am trying to add a level editor and want the user to be able to save their level to use [email protected]