Help on a summative formula

David Lees debl2nonospammywhamm at bellatlantic.net
Tue Jan 1 18:40:08 EST 2002


Your language is ambiguous, but it sounds like you are simply trying to
sum all inputs until the total reaches 150.  You post no sample code, so
assume your input is in a list, X.

def total(X):
    sum = 0
    for i in range(len(X)):
        sum += X[i]
        if sum >= 150:
            return sum

If what you intended to ask for was "how do I scan through input until I
have 2 successive inputs that total >= 150" then it is:

def prev2(X):
       for i in range(1,len(X)):
           if X[i-1]+X[i] >= 150:
               return X[i-1]+X[i]

Note this is sloppy code because there is no error condition checking.

David Lees

Ahimsa Consulting wrote:
> 
> Hi all
> Need newbie level assistance on putting together a formula. I am trying to
> work out the phrasing for reading user input and adding each input to the
> previous input until the total is >= 150. I thought for a while the
> Fibonacci series might help since that passes the value of one element and
> adds it to a previous value, so it worked fine for the first two inputs but
> then went off at a tangent.
> Any thoughts please.
> Thanks
> Andrew



More information about the Python-list mailing list