The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
if "name" then main()
Discuss if "name" then main() in the Python Programming forum on Dev Shed. if "name" then main() Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 24th, 2004, 06:04 PM
|
|
Contributing User
|
|
Join Date: Oct 2003
Location: Canada
Posts: 185
Time spent in forums: 20 h 49 m 17 sec
Reputation 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."
|

March 24th, 2004, 06:33 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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
|

March 24th, 2004, 07:53 PM
|
|
Contributing User
|
|
Join Date: Oct 2003
Location: Canada
Posts: 185
Time spent in forums: 20 h 49 m 17 sec
Reputation Power: 0
|
|
Thanks netyan. I fixed that. Now when i run the code i get the following Quote: 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.
|

March 24th, 2004, 11:05 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Posts: 84
Time spent in forums: 8 h 7 m
Reputation Power: 10
|
|
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 
|

March 25th, 2004, 06:34 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
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.
|

March 25th, 2004, 10:08 AM
|
|
Contributing User
|
|
Join Date: Oct 2003
Location: Canada
Posts: 185
Time spent in forums: 20 h 49 m 17 sec
Reputation Power: 0
|
|
Quote: | 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.
|

March 25th, 2004, 12:35 PM
|
|
Contributing User
|
|
Join Date: Jul 2003
Posts: 133
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
Quote: | 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.
|

March 25th, 2004, 05:21 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

March 25th, 2004, 08:16 PM
|
|
Contributing User
|
|
Join Date: Oct 2003
Location: Canada
Posts: 185
Time spent in forums: 20 h 49 m 17 sec
Reputation Power: 0
|
|
Quote:
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 ?
|

March 26th, 2004, 01:42 AM
|
|
Contributing User
|
|
Join Date: Jan 2004
Posts: 84
Time spent in forums: 8 h 7 m
Reputation Power: 10
|
|
|
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.
|

March 26th, 2004, 10:05 AM
|
|
Contributing User
|
|
Join Date: Oct 2003
Location: Canada
Posts: 185
Time spent in forums: 20 h 49 m 17 sec
Reputation Power: 0
|
|
Quote: | 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.
|

March 26th, 2004, 06:47 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|