0

I have two files. One named MainMenu.py another named MissionMenu.py for a simple text game I was making. But I ran into an issue (I'm still rather new to this) that is giving me a NameError:.

The first file looks like this

from MissionMenu import *
def mainMenu():
    x = input("Type Something:")
    missionMenu()
mainMenu()

The second File looks like this

from MainMenu import *
def missionMenu():
    x = input("Type something else:")
    mainMenu()
missionMenu()

The error says NameError: name 'missionMenu' is not defined

3 Answers3

0

look at this: Call a function from another file in Python

You are using import incorrectly. Yo don't need to add .py

from MissionMenu import missionMenu
def mainMenu():
    x = input("Type Something:")
    missionMenu()
mainMenu()

also, it's a bad idea in each file to import the other. kind of circular loop. Think which file really needs the other one. Probably they don't need to import each other

Roim
  • 2,986
  • 2
  • 10
  • 25
0

First of all you do not need a .py.

Suppose, If you have a file a.py and inside you have some functions:

def b():
  # Something
  return 1

def c():
  # Something
  return 2

And you want to import them in z.py you have to write

from a import b, c

Source and Details : Call a function from another file in Python

parlad
  • 1,143
  • 4
  • 23
  • 42
0

Two issues about your code:

Imports

To import a file called MainMenu.py:

import MainMenu

So do not use .py after that name.

Dependencies

You should keep your dependencies straight:

  • Do not import MissionMenu from MainMenu that imports MissionMenu
  • Just keep it simple ^^

Edit:

If you want to have multiple scripts that contain different menus, just create a function in those scripts (they are called modules then).

Import them in your main application (don't import the main script in the module) and run the function from the main file.

Hope this is helpful ^^

finnmglas
  • 1,626
  • 4
  • 22
  • 37