Posted By

denakitan on 10/25/12


Tagged

python main structure basics


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

navi4all


Python - Basic Main Structure


 / Published in: Python
 

URL: http://code.google.com/edu/languages/google-python-class/

Extremely simple snippet showing the basic structure of a Python program.

  1. import sys
  2.  
  3. def main():
  4. try:
  5. if len(sys.argv) >= 2:
  6. name = sys.argv[1]
  7. else:
  8. name = 'World'
  9. print 'Hello', name
  10.  
  11. print 'Type something to display its type and value.'
  12. userInput = raw_input()
  13. print 'User input - type: %s, value: %s' % (type(userInput), userInput)
  14. except Exception as ex:
  15. print ex.message
  16. return 1 # indicates error, but not necessary
  17. else:
  18. return 0 # return 0 # indicates errorlessly exit, but not necessary
  19.  
  20. # this is the standard boilerplate that calls the main() function
  21. if __name__ == '__main__':
  22. # sys.exit(main(sys.argv)) # used to give a better look to exists
  23. main()

Report this snippet  

You need to login to post a comment.