Home > Enterprise >  len() shows invalid syntax when define function
len() shows invalid syntax when define function

Time:12-02

def prodListePos_rec(l, len(l)):
    if (len(l)>0):
        if l[len(l)-1] > 0:
            product = prodListePos_rec(l,len(l)) * l[len(l)-1]
    else:
        product = 1 
    return product

l = [1,-2, 5, 0, 6,-5]
prodListePos_rec(l,len(l))      

I don't get why it shows the invalid syntax and what should I do if I want to call the len() as a recursion function?

CodePudding user response:

Function parameters must be identifiers, so l is fine, but len(l) is not.

Although, l is a bad variable name since it looks like 1 and I; you could use lst instead.

More importantly, you don't actually need to pass the len() around. You can simply get it inside the function.

Here's a fixed up version of your code. I added variables for values that are used more than once.

def prodListePos_rec(lst):
    n = len(lst)
    if n > 0:
        x = lst[n-1]
        if x > 0:
            product = prodListePos_rec(lst) * x
    else:
        product = 1
    return product

lst = [1, -2, 5, 0, 6, -5]
prodListePos_rec(lst)

However, the code still doesn't work because it's possible for n > 0 and x <= 0, so product never gets defined. Error:

Traceback (most recent call last):
  File "/home/wja/testdir/tmp.py", line 13, in <module>
    prodListePos_rec(lst)
  File "/home/wja/testdir/tmp.py", line 10, in prodListePos_rec
    return product
UnboundLocalError: local variable 'product' referenced before assignment

CodePudding user response:

When you define function, you write names of parameters in parethesis.

l is a name of parameter. When you call a function, python will "replace" l with it's value to calculate result of function.

len(l) is not a name, it's an expression. So you can't use it as a name.

You can change your function to this

def prodListePos_rec(l, len_l):
    if (len_l>0):
        if l[len_l-1] > 0:
            product = prodListePos_rec(l,len_l) * l[len_l-1]
    else:
        product = 1 
    return product

p = [1,-2, 5, 0, 6,-5]
prodListePos_rec(p,len(p))    

len_l is a name of parameter. I changed l to p in function calling, to not confuse variables with parameters

Or simply you can do that

def prodListePos_rec(l):
    if (len(l)>0):
        if l[len(l)-1] > 0:
            product = prodListePos_rec(l,len(l)) * l[len(l)-1]
    else:
        product = 1 
    return product

p = [1,-2, 5, 0, 6,-5]
prodListePos_rec(p)     

In this case len(l) is an expression, that calctulates length of a given list.

CodePudding user response:

def prodListePos_rec(l, len(l)): ->> len(l) is inbuilt function to find length of list, string, etc.

which cannot be used as variable to define the attribute of defined

function --> def prodListePos_rec(l, len(l)):

instead use

def prodListePos_rec(l, len_of_l):
    product = 1
    if (len_of_l)>0):
        if l[len_of_l)-1] > 0:
            product = prodListePos_rec(l,len_of_l)) * l[len_of_l)-1] 
    return product

l = [1,-2, 5, 0, 6,-5]
print(prodListePos_rec(l,len(l)))

Remember: this is a recursive function. So, It will not enter else block.

  • Related