Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 24th, 2004, 06:04 PM
caroundw5h caroundw5h is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 185 caroundw5h User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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."


Reply With Quote
  #2  
Old March 24th, 2004, 06:33 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #3  
Old March 24th, 2004, 07:53 PM
caroundw5h caroundw5h is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 185 caroundw5h User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old March 24th, 2004, 11:05 PM
rebbit rebbit is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 84 rebbit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old March 25th, 2004, 06:34 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to 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.

Reply With Quote
  #6  
Old March 25th, 2004, 10:08 AM
caroundw5h caroundw5h is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 185 caroundw5h User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #7  
Old March 25th, 2004, 12:35 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #8  
Old March 25th, 2004, 05:21 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #9  
Old March 25th, 2004, 08:16 PM
caroundw5h caroundw5h is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 185 caroundw5h User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 ?

Reply With Quote
  #10  
Old March 26th, 2004, 01:42 AM
rebbit rebbit is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 84 rebbit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #11  
Old March 26th, 2004, 10:05 AM
caroundw5h caroundw5h is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 185 caroundw5h User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #12  
Old March 26th, 2004, 06:47 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > if "name" then main()

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap