Owen Bradstreet Owen Bradstreet - 5 months ago 9
Python Question

Setting variables in a function and importing them

I'm writing an adventure game, and I'm writing functions in different files, such as the

saveGame
function and
read
function. There are 4 main files, all interconnected. The main one is
agMain.py
. My issue is that I've imported one of my other files
agRead.py
, and call the function within that called
read()
. Here is the data for the
agRead.py
:

import os
import operator
import sys
import agSave
import agCreation
def read():
while True:
try:
file = open("agData.txt", "r")
except:
exec(open("agCreation.py").read())
break
break
file = open("agData.txt", "r")
data = file.readlines(0)
encrypFName = data[0]
encrypSName = data[1]
encrypAge = data[2]
encrypGender = data[3]
encrypHealth = data[4]
encrypMaxHealth = data[5]
encrypArmour = data[6]
encrypMaxArmour = data[7]
encrypHealthPotions = data[8]
encrypExp = data[9]
encrypMaxExp = data[10]
encrypLevel = data[11]
encrypGold = data[12]
encrypMaxGold = data[13]
encrypPowerLevel = data[14]
encrypMaxPowerExp = data[15]
encrypPowerExp = data[16]
encrypStage = data[17]
fName = encrypFName.strip()
sName = encrypSName.strip()
age = encrypAge.strip()
gender = encrypGender.strip()
health = encrypHealth.strip()
maxHealth = encrypMaxHealth.strip()
armour = encrypArmour.strip()
maxArmour = encrypArmour.strip()
healthPotions = encrypHealthPotions.strip()
exp = encrypExp.strip()
maxExp = encrypMaxExp.strip()
level = encrypLevel.strip()
gold = encrypGold.strip()
maxGold = encrypMaxGold.strip()
powerLevel = encrypPowerLevel.strip()
maxPowerExp = encrypMaxPowerExp.strip()
powerExp = encrypPowerExp.strip()
stage = encrypStage.strip()


As you can see, it sets variables such as
stage
and
level
. My
agMain
files however:

import os
import operator
import sys
import agSave
import agRead
while True:
agRead.read()
if (stage == 1):
exec(open("agStage1.py").read())
elif (stage == 2):
exec(open("agStage2.py").read())
elif (stage == 3):
exec(open("agStage3.py").read())
elif (stage == 4):
exec(open("agStage4.py").read())
elif (stage == 5):
exec(open("agStage5.py").read())
elif (stage == 6):
exec(open("agStage6.py").read())
elif (stage == 7):
exec(open("agStage7.py").read())
elif (stage == 8):
exec(open("ageStage8.py").read())
elif (stage == 9):
exec(open("agStage9.py").read())
elif (stage == 10):
exec(open("agStage10.py").read())
elif (stage > 10):
for i in range(100):
print("Game over!")
print("****************")
print("Well done, you have completed the game!")
exit()
else:
print("An error has occured.")
continue


As you can see here, it uses the
stage
when it from the function in the code. But it can't. If I run this, it says stage is not defined, like I hadn't run the function. Is it possible to set variables in a function, use the function, and use the variables in creates in a different file?

Answer

The variables are only visible from inside read.

Contain the variables you want to use by doing something like this.

agRead.py

class Conf(object):
    pass


def read():
    while True:
        try:
            file = open("agData.txt", "r")
        except:
            exec(open("agCreation.py").read())
            break
        break
    file = open("agData.txt", "r")
    data = file.readlines(0)
    encrypFName = data[0]
    encrypSName = data[1]
    encrypAge = data[2]
    encrypGender = data[3]
    encrypHealth = data[4]
    encrypMaxHealth = data[5]
    encrypArmour = data[6]
    encrypMaxArmour = data[7]
    encrypHealthPotions = data[8]
    encrypExp = data[9]
    encrypMaxExp = data[10]
    encrypLevel = data[11]
    encrypGold = data[12]
    encrypMaxGold = data[13]
    encrypPowerLevel = data[14]
    encrypMaxPowerExp = data[15]
    encrypPowerExp = data[16]
    encrypStage = data[17]

    conf = Conf()

    conf.fName = encrypFName.strip()
    conf.sName = encrypSName.strip()
    conf.age = encrypAge.strip()
    conf.gender = encrypGender.strip()
    conf.health = encrypHealth.strip()
    conf.maxHealth = encrypMaxHealth.strip()
    conf.armour = encrypArmour.strip()
    conf.maxArmour = encrypArmour.strip()
    conf.healthPotions = encrypHealthPotions.strip()
    conf.exp = encrypExp.strip()
    conf.maxExp = encrypMaxExp.strip()
    conf.level = encrypLevel.strip()
    conf.gold = encrypGold.strip()
    conf.maxGold = encrypMaxGold.strip()
    conf.powerLevel = encrypPowerLevel.strip()
    conf.maxPowerExp = encrypMaxPowerExp.strip()
    conf.powerExp = encrypPowerExp.strip()
    conf.stage = encrypStage.strip()
    return conf

and agMain.py

import agRead
while True:
    conf = agRead.read()
    if conf.stage == 1:
Comments