-2

So I'm very new to Python and programming in general. I've run into an issue with my text game. I'm trying to create a function (pretty sure 'def (name) is a function, right?)in a .py called 'locationMenu' and use it in my main game file. Here is the start of my locationMenu.py (the rest is simply prints and if/elif/else statements.

from main import Engine, Hallway, Canteen, Bedroom, Room

class locationsMenu():
    def locationMenu(self):

and here is the place in my main game file where I am trying to use the 'locationMenu' function that I just created.

    locationMenu()

That is just one line after I made a choice with input, etc. but now I would like to skip to the locationMenu that is in the other file.

Any help much appreciated. Please don't use "big words" because as I said I'm still very new to programming in general.

Anthony-y
  • 1
  • 1
  • Try to keep your programs to one file ("module") when you're starting out. Experiment with packages and modules later! :) – Adam Smith Sep 20 '14 at 19:26
  • you named your `module`, `class` and `method` in the class `locationMenu`? I am not sure if it's a good practice or not. – salmanwahed Sep 20 '14 at 19:32
  • And by the way, it's not clear from your question, but if you are trying to call `locationMenu()` from within `main.py`, and `locationMenu.py` is also importing stuff from `main.py`, then there's probably a better way of structuring your code. Here's [a good discussion on cyclic imports](http://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python). – Crowman Sep 20 '14 at 19:38
  • @Adam Smith I really tried to keep it to only one or two files but I ran into isues with the rooms. I had setup a room class and made all my other rooms inherit this class but I found that I had to call functions before even defining them. In the end I just thought that if the rooms were in another file (world.py) then it would be easier to call them up whenever I needed because they were already defined in a different place. Maybe you could help me out with that? – Anthony-y Sep 21 '14 at 09:23

1 Answers1

2

locationMenu() is an instance method of the locationsMenu() class, so you'd need to create an instance and call the method on it, something like:

from locationMenu import locationsMenu

my_menu = locationsMenu()       #  Create a new object
my_menu.locationMenu()          #  Call its instance method

If you were to just try locationsMenu.locationMenu() then you'd get something like this error:

Traceback (most recent call last):
  File "./prog.py", line 3, in <module>
    locationsMenu.locationMenu()
TypeError: locationMenu() missing 1 required positional argument: 'self'

because locationMenu() is not a class method, and you're trying to call it in the absence of a locationsMenu object.

If you don't want it inside a class at all, then make it a regular function, and do:

locationMenu.py:

from main import Engine, Hallway, Canteen, Bedroom, Room

def locationMenu():
    print("In function locationMenu()")

prog.py:

from locationMenu import locationMenu

locationMenu()
Crowman
  • 25,242
  • 5
  • 48
  • 56
  • Just returns this: `Traceback (most recent call last): File "main.py", line 12, in from locationMenu import locationsMenu File "A:\Python Text Adventure Eclipse Workspace\Surface_tension\locationMenu. py", line 1, in from main import Engine, Hallway, Canteen, Bedroom, Room File "A:\Python Text Adventure Eclipse Workspace\Surface_tension\main.py", lin e 12, in from locationMenu import locationsMenu ImportError: cannot import name 'locationsMenu' Press any key to continue . . .` @Paul Griffiths – Anthony-y Sep 20 '14 at 20:29
  • @user4061908: Then there's either something wrong with your implementation, or another problem in the code you're not showing us. – Crowman Sep 20 '14 at 20:33
  • Should I post **ALL** of the main.py and locationMenu.py code then? Knowing me I've probably implemented it wrong. @Paul Griffiths – Anthony-y Sep 20 '14 at 20:50
  • @user4061908: You should try making it work with the smallest possible implementation, i.e. pretty much just the two short files in my answer, or something similar. If that works for you, then you know your problem is something else, and the task becomes finding out exactly what that something else is. – Crowman Sep 20 '14 at 21:45