开发者

Importing modules inside of modules in Python

开发者 https://www.devze.com 2023-02-15 02:29 出处:网络
I\'m trying to create a hierarchy of options.I\'m able to creat开发者_Python百科e the first list of options:

I'm trying to create a hierarchy of options. I'm able to creat开发者_Python百科e the first list of options:

  1. lemur
  2. gorillas
  3. chimps

if the user chooses option 1 for lemurs then I run the Lemur.LE() function cause I already imported the lemur module. They are then presented with another set of options:

  1. Brandy
  2. Cigars
  3. Shaving Cream
  4. Choose a different monkey.

option 4 runs a break which sends them back to the first list. I'm trying to repeat actions from the first architecture so that I can have them choose Shaven.SC() by importing when the function Lemur.LE() is called, but if I place the imports before the function starts then I get a fatal crash at the beginning when I first import lemur, if I call them from within' the LE() function then I get a strange indention exception. Thoughts? Am I making this harder on myself then necessary?

P.S.

Okay here's the Code:

begin = int(raw_input("""Options 1-6"""))
    elif begin == 3:
    L.Leg()
    elif begin == 6:
        print "Goodbye"
        exit()

Level 2:

def Leg():
    begin = int(raw_input("""options 1-5"""))
    elif begin == 2:
        import LegacyWT
    else:
        print "Returning to Main Menu."
        break


get a strange indention exception. Thoughts? Am I making this harder on myself then necessary?

Indentation exceptions are almost always caused by mixing tabs and spaces in the same file. If you are using a decent editor, you can set it to automatically convert tabs to spaces. If you are not using a decent, stop it, and use a decent editor.


It's not clear what you are doing wrong without a code sample. In general import is a statement like any other and can be used in any scope. So:

def LE():
    ...
    if option == 'Shaving Cream':
        import Shaven
        Shaven.SC()
    elif option == ...

should work just fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号