Home python TypeError: 'int' object is not callable

TypeError: ‘int’ object is not callable

Author

Date

Category

I tried to write sorting a list using recursion, I started getting an error TypeError: 'int' object is not callableafter adding a function recursion (before that I used lists of three elements for tests – recursion was not needed) .

def a(mas):
    menshe = []
    bolshe = []
    sorted = []
    if len(mas) <= 2:
        return mas
    else:
        a = int(len(mas) // 2)
        vibor = mas[a]
        del mas[a]
        if len(mas) == 2:
            for i in mas:
                if i < vibor:
                    menshe.append(i)
                else:
                    bolshe.append(i)
            sorted.append(menshe)
            sorted.append(vibor)
            sorted.append(bolshe)
            print(sorted)
        else:
            a(mas)

Sorry for the shit code, just learning)


Answer 1, authority 100%

The function is named the same as the internal variable.

Thus, when on the last line you “call” a(mas)for the program at that moment ais already reassigned and there is a number (from the line a = int(len(mas) // 2)).

To avoid such errors, please read code formatting conventions and do not use short a, b, i, j, kand other names, but use meaningful names of variables and functions. Functions usually use a verb or an action description.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions