-4

I've make two separate program and make a main.py to as menu so that i can choose what file to open.

I wrote it like this

While True:
   print("Menu 1")
   print("Menu 2")
   ...

   choice = input(">")
   if choice =="1":
      import file_1
   elif choice =="2":
       import file_2

At first it work, but when get out of file_1 or any file inside that, the import are not open again.

Does import cannot open file twice?

  • No Imports should be carried out at the start according to the pep convention but one import can only import a module once. – Navaneeth Reddy Nov 29 '20 at 13:09
  • It's hard to tell from this, but I don't think import does what you think it does. Try reading this https://docs.python.org/3/reference/import.html. it's not used to "call" code in other files, but to allow you to use functions, classes, etc declared in other files. – dpwr Nov 29 '20 at 13:11

1 Answers1

0

You can import both files and use it based on your condition.Something like this.

import file1
import file2
def main():
      print("Menu 1")
      print("Menu 2")
      choice = input(">")
      if choice =="1":
          // use instances of file_1 e.g  
               result = file1.function1(x,y,z)

      elif choice =="2":
          //use instances of file 2

main()