#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0

    if "name" then main()


    I have the following code
    Code:
    def Div():
        print "N/A AS YET!"
        raw_input()
    
    def Main():
        import random
        correct, wrong = 0,0
        menu()
    
    
     if __name__== '__main__' : Main()
    however when i try to run it it gives me a synatax error"unindent does not match any outer indentation level"
    I'm wondering if i'm using the if __name__ attributes correctly. My code consist of about 4 more funcitons all of which can be accesed through Main(). I also don't have it stored in pyhon's path. Should that matter?
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The problem is pretty simple if you set your editor to show white space you shouldnt have any problem spotting it. But just incase you dont have an editor which can do this; you have a space at the beginning of the last two lines...

    Code:
    .if.__name__== '__main__'.:.Main()
    .
    Note: spaces are indicated by the dots.

    Which is thowing the indentation off, if you remove this then it should work fine.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0
    Thanks netyan. I fixed that. Now when i run the code i get the following
    minuend = random.randrange(100)
    NameError: global name 'random' is not defined
    isthis a whitespace issue again.
    Code:
    def Main(correct = 0, wrong = 0):
        import random
        menu()
    
    if __name__== '__main__' :Main()

    I changed it to this
    Code:
     if __name__=='__main__': 
          import random
          Main()
    and it works fine. Any ideas what i was doing wrong?
    Last edited by caroundw5h; March 24th, 2004 at 07:59 PM.
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    you need to import a module to load its functions/classes/etc into the current namespace for use in your script. the only thing wrong was that you weren't importing the random module, but now that you are all works fine
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Rebbits right, if you import a module inside a function then it gets imported into the local namespace so wont be accessable though another function. One solusion is to pass random in as an argument although i find its best to import everything in one go at the top of my program.

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0
    Originally Posted by netytan
    Rebbits right, if you import a module inside a function then it gets imported into the local namespace so wont be accessable though another function. One solusion is to pass random in as an argument although i find its best to import everything in one go at the top of my program.

    Mark.
    Weird. for some reason I thouht random would be in the global scope. I guess the only way would be to reload it in every function i call then right?
    Thanks for your input.
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    Originally Posted by caroundw5h
    Weird. for some reason I thouht random would be in the global scope. I guess the only way would be to reload it in every function i call then right?
    No. To have random in the global scope means you should import it in the global scope. put "import random" at the very top of your script.
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    If you want to have a module directly accessable to all it has to be in the global namespace although since imported modules are bound to variable names you can pass them around as such. Two ways around your problem, starting with my favourate.

    Code:
    #!/usr/bin/env python
    
    import random
    
    def something():
        return random.randint(5, 10)
    Or passing the random module as an argument (check my last post. sure i mentioned it )

    Code:
    #!/usr/bin/env python
    
    def something(name):
        return name.randint(5, 10)
    
    def passmod():
        import random
        something(random)
    I'm not really sure why you feel you have to put everything in functions but if it helps you think go for it .

    Mark.
    programming language development: www.netytan.com Hula

  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0

    I'm not really sure why you feel you have to put everything in functions but if it helps you think go for it .

    Mark.
    Are you talking about my code? you mean versus a class ?
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    I think he means vs just straight forward procedural code. because Python offers functions/classes/etc doesn't mean you must make use of them. if using functions or classes is obviously cluttering up your code, and you don't feel you need to code it for reuse later on, then you shouldn't. just do whatever feels natural to you - don't go out of the way to use a particular paradigm.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0
    Originally Posted by rebbit
    I think he means vs just straight forward procedural code. because Python offers functions/classes/etc doesn't mean you must make use of them. if using functions or classes is obviously cluttering up your code, and you don't feel you need to code it for reuse later on, then you shouldn't. just do whatever feels natural to you - don't go out of the way to use a particular paradigm.
    I'm use to codiing in C where functions simplyfy coding. how would you all have done it.
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  22. #12
  23. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Rebbit ya got it in one . Basically anything you would write in your main() function, dont. you can put this inside a standard 'if running' statment; this will only be called if the program is run and is more efficent, not to mention cleaner than writing a main() function in each program . For example...

    Code:
    ...
    if __name__ == '__main__':
        import random
        menu()
    With me here dude? Well hope this is of some use to you,

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo