Andrew Kyriakou
Andrew Kyriakou

Reputation: 13

I dont know how to deal with this python error TypeError: 'tuple' object is not callable

I have searched other threads and questions with the similar problem and from what i see the problem arises when your variables have the same identifier as a built in function, but i have tried or sorts of name for my identifiers it still didn't work... I am very new to python so i may not be familiar with complicated terminology used i python

grades = [('A', 90), ('B', 70), ('C', 50), ('D', 30), ('F', 0)]
while True:
    score = int(input('Enter grade: '))
    for counter in grades:
        if score >= counter(2):
            fingrade = (counter(1))
            break
    print('This student got an ' + fingrade)

this is the error

if score >= counter(2):
TypeError: 'tuple' object is not callable

Upvotes: 1

Views: 472

Answers (2)

AJ_
AJ_

Reputation: 1485

You are accessing elements in the tuple incorrectly. Use square brackets instead of round ones, as round brackets imply a function call. By writing counter(2) Python thinks that you are trying to call a function counter with 2 as an argument. However, counter represents a tuple which is not callable.

Also, upon fixing this error you'll notice that your indexes are off by 1, as tuple indexes are 0-based.

The following code should fix the problems.

grades = [('A', 90), ('B', 70), ('C', 50), ('D', 30), ('F', 0)]

while True:
    score = int(input('Enter grade: '))
    for counter in grades:
        if score >= counter[1]:
            fingrade = (counter[0])
            break

    print('This student got an ' + fingrade)

Upvotes: 0

Alexander Pivovarov
Alexander Pivovarov

Reputation: 4990

You need to access the elements using square brackets like this counter[2]

When you write counter(2) python thinks you want to call counter as a function hence the error.

Also tuples are zero-indexed, so first element is actually counter[0] and second is counter[1].

Upvotes: 2

Related Questions