python quick question

Soldato
Joined
12 May 2004
Posts
7,018
Location
England
this function returns an empty list how why?



Code:
def calc (amt,total):
    r =[]
    t = total
    amt = amt
    c = len(amt)
    

    for i in range(c):
        r.append(float(amt[i]/sum(amt)*t))
        i = i+1


    return(r)
 
I would have to see what you're passing as inputs but I would guess that it's because you're initialising r to an empty list and what ever your input is for amt it's not a list or string so it's not entering the loop. The i = i + 1 is completely unnecessary, I'm not even sure how that effect the range statement.

As an aside that is the craziest python I've ever seen. You could literally condense that to one line. Look up List Comprehensions in the python docs.
 
Back
Top Bottom