I’m sure all experienced Python programmers know a simple, elegant way to do this. Unfortunately, at my beginner’s level, I do not.
Anyway, I’m currently attempting to modify some AI scripts for a game program written by others. I’m looking for a way to extract the numeric elements from an existing object and then add all of those elements together.
The number of elements in the object will vary, but will usually be no more then ten (10).
The elements in the object will always be ‘floats’.
So, for i in obj:
How should I proceed to extract all of the elements from the existing object and then add them together to return a new ‘float’ number?
You can use
sum(iterable)to add up all the items in a tuple or list.sum([1,2,3])returns6, for example.