Created
September 5, 2011 08:59
-
-
Save fnielsen/1194493 to your computer and use it in GitHub Desktop.
Small Python demonstration program to add numbers from input argument
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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