[Tutor] Alternative to for/while loop

niraj pandey nirajkumarpandey at gmail.com
Tue Oct 25 04:09:17 EDT 2016


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



On Tue, Oct 25, 2016 at 7:08 AM, Bryon Adams <bryonadams at openmailbox.org>
wrote:

> Hello,
>     I'm very new to python so please forgive what may be a beginner
> question. The book I'm working through hasn't covered using flow control
> yet so I'm thinking there should be a way to do this without the for loop I
> used, but I'm at a loss here. So far the book has covered: lists, strings,
> numerical types (float, integer, etc), methods, tuples, importing modules,
> boolean logic, and mathematical operators.
>     The problem asks to receive an arbitrary list of numbers separated by
> commas and then find the average. If someone could point me in the right
> direction I'd appreciate it. I'm using Python3.5.1 on Fedora 24 Linux,
> though the book uses Python2.x.
>
>   My code:
> nums = input('Enter some numbers separated by commas: ')
> nums = [float(i) for i in nums.split(', ')]
> print((sum(nums)) / len(nums))
>
> Regards,
>   Bryon
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Success occurs when opportunity and preparation meet


More information about the Tutor mailing list