[Tutor] Running python from windows command prompt
Asokan Pichai
pasokan at talentsprint.com
Thu Apr 11 08:24:17 CEST 2013
On Thu, Apr 11, 2013 at 11:38 AM, Arijit Ukil <arijit.ukil at tcs.com> wrote:
> Thanks for the help. Now I have modifed the code as:
>
> import sys
>
> def main(argv):
>
> data = int(sys.argv[1])
>
> avg = average (data)
>
> print "Average:", avg
>
> def average(num_list):
> return sum(num_list)/len(num_list)
>
> if __name__ == "__main__":
> main(sys.argv[1:])
>
Two major problems:
One:
The __main__ methods processes (correctly) the args and drops argv[0]
But your main() function is calling sys.argv again
Two:
sum() and len() expect a list -- more accurately an iterable.
when you say
data = argv[1]
data is a scalar.
If you know of list comprehensions, it can be easier or else,
think of a function to convert a list of strings (argv[1:] to a list
of floats; more general than int)
--------------------
def arg2list(arg):
args = []
for a in arg:
args.append(float(a))
return args
--------------------
Now replace the data = line with
data = arg2list(argv) # NOT sys.argv
HTH
Asokan Pichai
"Expecting the world to treat you fairly because you are a good person is a
little like expecting the bull to not attack you because you are a
vegetarian"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130411/1b93846d/attachment.html>
More information about the Tutor
mailing list