1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Rep Power

    How would I switch python files and run code from those files?

    I'm fairly new to Python, with a fairly simple question. I'm trying to make a simple text game in Python, kinda like Zork, just to play around with the code. I wondered if I could do something like this and how:

    there'd be a few files, like this:


    And this would happen. start.py would run fine and dandy, then when a certain thing happens (such as the player proceeds far enough for a certain variable to become True), it would run otherfile.py, instead of having all of the levels in one file. Is this possible? And how about a global variable shared between all files, such as the player's name, is that possible? I understand python syntax and understand enough to do some things within one file, but nothing like using multiple files.

    Storing certain things like journal entries for when the player picks one up to read in its own file would be pretty neat too.

    Sorry if I sound like a noob because I pretty much am, lol. Don't be harsh. This is also probably more work than i'm making it sound.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Rep Power
    Probably your best bet would be:
    1. In each file, place all the code you want to run into these functions.
    2. Create a new file, say called main.py, which imports each of your other files.
    3. From main.py, call the functions you defined in step 1.

    Anything "global" can be placed in main.py and passed as an argument to your functions.

IMN logo majestic logo threadwatch logo seochat tools logo