[Tutor] Alternative to for/while loop

Bryon Adams bryonadams at openmailbox.org
Mon Oct 24 21:38:10 EDT 2016


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


More information about the Tutor mailing list