Python - Basic Main Structure


/ Published in: Python
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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()

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.