Skip to content

Instantly share code, notes, and snippets.

@fnielsen
Created September 5, 2011 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fnielsen/1194493 to your computer and use it in GitHub Desktop.
Save fnielsen/1194493 to your computer and use it in GitHub Desktop.
Small Python demonstration program to add numbers from input argument
#!/usr/bin/env python
import math, sys # Importing modules.
def formatresult(res): # Define function. Remember colon!
"""This is the documentation for a function."""
return "The result is %f" % res # Percentage for formating
if len(sys.argv) < 3: # Conditionals should be indended
print("Too few input argument")
elif len(sys.argv) > 10: # Not 'elsif' or 'elseif'
print("Too many input argument")
else:
res = 0; # Semicolon not necessary. Considered bad style
for n in range(1, len(sys.argv)): # Not first element in loop
try: res += float(sys.argv[n]) # One-liner: no identation
except: pass # One-liner!
print(formatresult(res))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment