Ask coding questions

← Back to all posts
I keep getting an error! No explanation online will help! Could somebody help?
h
NoelB33

https://repl.it/@NoelBryan1/PyLife

Basically, I’m trying to get a number from a leaderboard to test if you can get on the leaderboard. It is at the end of the code, around line 750, and I keep getting an error! It’s a weird error, and I don’t understand it at all. It only happens when you die in my game, and out in your name. Also, I can not figure out how to get stuff from a text file, so could somebody please help me? I’m new to coding, so I don’t understand a lot of stuff. Thanks so much!

Answered by AdCharity [earned 5 cycles]
View Answer
Voters
NoelBryan1
AtriDey
NoelB33
Comments
hotnewtop
AdCharity

well, I'm not going to go read through hundreds of lines of code, but here are a few tips.

Reading File

Debug Tips

  1. Check that variables are in the proper scope (meaning they are either global or just accessible depending on the location of the variable)
  2. Read the error message. What is it saying? Usually, error messages can track down what's happening (unless it's javascript).
NoelB33

@AdCharity, The error says type error:_io.TextIOWrapper object is not subscriptable

AdCharity

@NoelBryan ok now I have to read through the code because that error has something to do with reading files or something similar

Warhawk947
NoelB33

I noticed I never selected your answer, even though it worked for me, sorry. @AdCharity

AdCharity

@NoelBryan lol it's fine happy programming :P

mwilki7

try printing out what 'f' is
near:

mwilki7

it looks like the file object doesn't play nice with [] indices
try turning it into an array:

https://stackoverflow.com/questions/28977477/typeerror-io-textiowrapper-object-is-not-subscriptable

NoelB33

Thanks! @mwilki7

NoelB33

Actually now I’m getting an error saying: list index out of range. I’m just trying to get the characters 16, 17, and 18 and I think it’s getting lines 16 17 and 18. I looked this up again but there is no answer to it. @mwilki7

mwilki7

@NoelBryan
try printing the contents of line just to see if the file open is working

Now that I think about it, lines should be an array of strings.

line[16] would mean line 16 in ur file which would not work if ur file is not 16 lines long

What do you want to do with the contents of the file?

NoelBryan1

Sorry, I’m on a different account because my pc and phone have different stuff, but I want to get the characters 16, 17, and 18 and combine them. I’m trying to text if you qualify for a leaderboard by getting the current recordholder’s score @mwilki7

NoelBryan1

@mwilki7 Oh and also, the code above works, (your code), and it prints The whole lines