[Tutor] Alternative to for/while loop

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Tue Oct 25 05:14:26 EDT 2016


On 25.10.2016 10: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
>

better try things yourself before suggesting them to others:

 >>> my_string = '0,1,2,3'
 >>> my_lst=my_string.split(",")
 >>> leng = len(my_lst)
 >>> b=sum(my_lst)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

This is exactly why the OP had to come up with his comprehension solution.


More information about the Tutor mailing list