[Tutor] Alternative to for/while loop

Alan Gauld alan.gauld at yahoo.co.uk
Tue Oct 25 05:33:35 EDT 2016


On 25/10/16 09:09, niraj pandey wrote:
> Try this.
> 
> my_string = '0,1,2,3'
> my_lst=my_string.split(",")
> leng = len(my_lst)
> b=sum(my_lst)
> avg=float(b/leng)
> print avg

It doesn't work because my_list is a list of strings and you can't sum()
strings. The loop is needed to convert the strings to numbers.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list