Home > Net >  'list' object is not callable, Python error
'list' object is not callable, Python error

Time:03-31

I am trying to make a python function that receives two integers and returns a list of a descending order starting from the largest integer. (ex: f(2,5)=[5,4,3,2])

This is what I tried:

def f(n1: int, n2: int) -> List(int):
    if n1>n2:
        L=list(range(n2,n1 1))
        L.reverse()
    elif n1==n2:
        L=[n1]
    else:
        L=list(range(n1,n2 1))
        L.reverse()
    return L

the problem is I keep getting an error saying "'list' object is not callable" at List(int). What is wrong with List(int), please?

CodePudding user response:

There is syntax error in type annotation. For more info I recommend to read official docs about it https://docs.python.org/3/library/typing.html. To solve it you need to change List(int) to List[int]. In python the () operator is so call callable operator and performs e.g function call like f(1, 2)

CodePudding user response:

L is a object you have created.you should call the function f.

def f(n1: int, n2: int):
    if n1>n2:
        L=list(range(n2,n1 1))
        L.reverse()
    elif n1==n2:
        L=[n1]
    else:
        L=list(range(n1,n2 1))
        L.reverse()
    return L
f(2,5)
  • Related