Python Imports

  • bgraybr
  • Novice
  • Novice
  • bgraybr
  • Posts: 26

Post 3+ Months Ago

I'm trying to make a python code that does the same thing as this but shorter:

Code: [ Select ]
 
try:
    x = save.level
    if x == 1:
        import level1
        import level2
        import level3
    elif x == 2:
        import level2
        import level3
    elif x == 3:
        import level 3
except:
    print 'error- cannot import levels'
 
  1.  
  2. try:
  3.     x = save.level
  4.     if x == 1:
  5.         import level1
  6.         import level2
  7.         import level3
  8.     elif x == 2:
  9.         import level2
  10.         import level3
  11.     elif x == 3:
  12.         import level 3
  13. except:
  14.     print 'error- cannot import levels'
  15.  


So I thought about using something like this, but it would just try to import a module named x.

Code: [ Select ]
 
x = save.level
levlist = [1, 2, 3,]
while x <= 3:
    if x in levlist:
        print x, 'was found.'
        import x
        x -= 1
    else:
        print x, 'was not found.'
 
  1.  
  2. x = save.level
  3. levlist = [1, 2, 3,]
  4. while x <= 3:
  5.     if x in levlist:
  6.         print x, 'was found.'
  7.         import x
  8.         x -= 1
  9.     else:
  10.         print x, 'was not found.'
  11.  


I'm a little stuck, is the first code the only thing that will really work?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: No registered users and 127 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.