Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

Javascript implementation in my python code, so that variables stored in the external .JSON file can be used as global variables

I have done this so far to implement Javascript in my code. The variables are stored in the external .json file just fine. How would I make these variables global variables, so that they can be used in different functions?

with open("file.json" , "r") as f :
  data = json.load(f)

if data == {} :
    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)

else :
    monthlyWage = data["monthlyWage"]
    monthlySpendings = data["monthlySpendings"]
    monthlyOverdraft = data["monthlyOverdraft"]
    monthlySavings = data["monthlySavings"]
    totalSpendings = data["totalSpendings"]
question from:https://stackoverflow.com/questions/66063749/javascript-implementation-in-my-python-code-so-that-variables-stored-in-the-ext

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...