[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

The sum of ten numbers inserted from the user

Bart <bc at freeuk.com> writes:

> On 07/02/2019 20:45, Ben Bacarisse wrote:
>> Ian Clark <ianhclark510 at gmail.com> writes:
>>> This is my whack at it, I can't wait to hear about it being the wrong big o
>>> notation!
>>> numbers=[]
>>> while len(numbers) < 10:
>>>      try:
>>>          chip = int(input('please enter an integer: '))
>>>      except ValueError:
>>>          print('that is not a number, try again')
>>>      else:
>>>          numbers.append(chip)
>>> print(sum(numbers))
>> Why would you not keep a running total, rather than list of the numbers?
>> I've not been following assiduously, so maybe I missed some other
>> requirement...
> Because it separates the two tasks: (A) capture N values; (B) perform
> some operation on those values, in this case summing (and taking
> advantage here of the built-in sum()).

Sure.  Did I miss some hint that this program might need to be more
flexible like that?  It looks like a beginners' exercise.

If the purpose is to be flexible, why is an array the right option rather
than, say, accumulating a value over an iterable?  You need some idea of
the future direction or what the exercise is intended to reinforce.