0

I have two python files:

  1. Term
  2. Today

In the term.py file, the user is to enter some data For instance, in the term.py file, the user is to enter the age,sum_assuared, and the duration. How can I make today.py file have access to the data variables(age,sum_assuared and duration) which have been input by the user in the term.py file instead of asking them to input the variables again in the today.py file

In the term.py file, I have this code

 import today
 def Term_advance():
     print('advance')
     n = int(input('enter duration (n) :'))
     age = int(input("enter life's age :"))
     Sum_assuared = int(input('enter sum assuared :'))
     basis = input('what is the calculation basis::\n'
               '1.\t AM 92\n'
               '2.\t ELT 15 MALES\n'
               '3.\t ELT 12 FELAMES\n'
              '4.\t PEN\n'
              '5.\t A1967-70\n')
     If basis==1:
          today.tables_advance_perpetuity()

Then in the today.py file, I have this code that reads the values from an excel document. The variables here, however, are to already input by the user in the Term.py file(age,n, and sum_assuared)

 import xlrd
 loc = "C:PycharmProjects/Life annuities/tables.xlsx"  
 wb = xlrd.open_workbook(loc)


 def tables_advance_perpetuity():
     sheet = wb.sheet_by_index(0)
     sheet.cell_value(0, 0)
     num = int(input('enter the age of the life:'))
     for i in range(sheet.nrows):
         x = sheet.cell_value(i, 0)
         if x == num:
             dx = sheet.cell_value(num - 1, 2)
             nx = sheet.cell_value(num - 1, 6)
             print(dx)
             print(nx)
             ann = nx / dx
             print(f'Annuity value is {ann}')

So like for this case how can the today file read, for instance, the age,sum_assuared and n of the life entered by the user in the term.py file( instead of asking the user to enter the values again in the today.py file)

Like I seek to be able to read the variables(age,sum_assuared, and n) which are in the term.py file.So I wish to access them using the today.py file. instead of having a second prompt asking for the values in the today.py file

Victor
  • 25
  • 5

3 Answers3

0

The easiest way might be to save the input of the first script to an external txt file, and then read it in the second script.

Or you could try to create a third script that import both modules and run them in the same session.

There seems to be some ideas here: How to share variables across scripts in python?

VicN
  • 311
  • 2
  • 7
0

Well it depends if they are different processes or not, you could simply make a "main" function that calls the two files one after another and stores the information.

Otherwise if you want them to be two separate processes/programs you could use the module pickle to save the information in a .pickle file and then open the file in the other program.

Have a look at pickle if you are using objects with multiple attributes or such: https://docs.python.org/3/library/pickle.html. If you simply want to save a single value a text file should do the trick.

JakobVinkas
  • 1,003
  • 7
  • 23
0

You can import term.py as a module and make it return values. Modify term.py as follows (add to the end of Term_advance()):


    return age, n, sum_assuared

Then in the other script import the module and run the function to get values for variables (here I'm assuming both scripts are in the same folder):

import term

age, n, sum_assuared = term.Term_advance()

This way the user will need to enter data only once, but it will run through both scripts.

Alternatively, if you just want to run term.py separately, you can simply write values into a file and then have the second script read from it.

NotAName
  • 3,821
  • 2
  • 29
  • 44