You can add all that logic into a function which returns those variables and the import them anywhere like:
something.py
import json
def get_data():
with open("file.json" , "r") as f :
try:
data = json.load(f)
except json.JSONDecodeError:
monthlyWage = int(input("What is your monthly wage? ")) #Allow the user to input their monthlyWage
monthlySpendings = int(input("What are your monthly spendings? ")) #Allows the user to input their monthlySpendings
monthlyOverdraft = int(input("What is your monthly overdraft? ")) #Allows the user to input their monthlyOverdraft
monthlySavings = int(input("How much do you add into your savings account per month? ")) #Allows the user to input their monthlySavings
totalSpendings = int(monthlySpendings + monthlyOverdraft + monthlySavings) #Calculates the totalSpendings
data = {
"monthlyWage": monthlyWage,
"monthlySpendings": monthlySpendings,
"monthlyOverdraft": monthlyOverdraft,
"monthlySavings": monthlySavings,
"totalSpendings": totalSpendings
}
with open("file.json" , "w") as f :
json.dump(data , f , indent = 4)
# we convert the values of a dict to a list
return data.values()
and then in main.py
from something import get_data
def print_something():
# it will print values
print(get_data())
monthlyWage, monthlySpendings, monthlyOverdraft, monthlySavings, totalSpendings = get_data()
print(monthlyWage, monthlySpendings, monthlyOverdraft, monthlySavings, totalSpendings)
if __name__ == '__main__':
print_something()
I also modified a bit your logic as you may see as if you read from an empty file it would have thrown an error.
Let me know if this is what you were aiming for
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…