How to fix TypeError: "file" object is unsubscribed

I like the new python and I really don't understand my problem, really appreciate the help. Either way, this is the coding line.

def Banker(warrior):
    gold = open(chairs[warrior-1], "strength")
    return gold

      

This is the error I received.

line 22, in Banker
    gold = open(chairs[warrior-1], "strength")
TypeError: 'file' object is unsubscriptable

      

http://pastebin.com/1wMbaSYY

+3


source to share


2 answers


It looks like on your pastebin link, on line 19, in toyota()

you have:

return chances, Tire, Km, Insurance, chairs

      

which returns all those values ​​in a tuple (even without the parentheses). But this is called on line 58:



chances, chairs, insurance, km, tire = toyota()

      

Assigning values ​​from the returned tuple to the variables specified on the left. These tuples must be in the correct order. Here you are using the value Tire

as chairs

.

0


source


Found your problem. Line 58 is what is causing you problems. Here he is:

chances, chairs, insurance, km, tire = toyota()

      

There is nothing wrong with that, but when we look at the return statement toyota()

, a problem arises. Here is the return statement toyota()

:

return chances, Tire, Km, Insurance, chairs

      



The problem comes from the fact that order matters when you return multiple values ​​in Python, and the order in which you return and the order in which you assign does not match.

This is where you return a value Tire

and assign it to a variable chairs

. Later, when you try to use chairs

in a function Banker

, you are not working with the object you think.

gold = open(chairs[warrior-1], "strength")

      

This is where the launch type(chairs)

will return File

, not list

because the values toyota()

were returned / assigned in the wrong order. File

cannot be indexed, which is what causes your program to throw an error.

0


source







All Articles