0

Quick feedback on error?

Here's the code: https://code.sololearn.com/cJ6Mf69XnMF8/#py Problem is at line 30: length = wordlist.index('\n')+1 Error I'm getting: ValueError: '\n' is not in list Code is supposed to open and read a user input file which is a word puzzle grid, and then append the lines from the file to a list and search for specific words through that list.

21st Dec 2017, 10:59 AM
Laura
Laura - avatar
13 Answers
+ 2
If you get it in playground problem is that there are no .txt files there, so there will be no lines in list, so no line brakes. Edit: if you have this in ide, may be reason a parameter in open method, you appending file not reading... as far as I understand.
21st Dec 2017, 11:04 AM
Dima Makieiev
Dima Makieiev - avatar
+ 1
Correct me if I wrong in 'for line in wordlist' you refer to lines that arent there yet, since your file opens to the list named lines, or not? I am only learning Python currently
21st Dec 2017, 11:32 AM
Dima Makieiev
Dima Makieiev - avatar
+ 1
you also need to have some value in the "line" variable in the first place for this to work. try something like (depending on what you want to do) f=open(filename.txt) line=f.readline() or line=f.read() this will put content from the file into the variable "line" then you can split it or whatever.
21st Dec 2017, 11:47 AM
ifl
ifl - avatar
+ 1
Look line by line 1 you declare empty list "wordlist=[] " 2. you open file and store it as variable lines 3. for each line in "wordlist " do append but, there are no lines there. You need to try "for line in lines" append line to wordlist.
21st Dec 2017, 11:58 AM
Dima Makieiev
Dima Makieiev - avatar
0
Yeah the error is in idle. It can print and display the grid properly but doesn't go beyond that part. I thought the reading of the file was right hm so confused.
21st Dec 2017, 11:20 AM
Laura
Laura - avatar
0
Derp. I edited to append before I do the rest. Still same error.
21st Dec 2017, 11:27 AM
Laura
Laura - avatar
0
The list I named 'wordlist'. The file to be opened is stored in 'files' variable. I'm not sure what you're asking about lines?
21st Dec 2017, 11:35 AM
Laura
Laura - avatar
0
Ifl, I tried that. Still same error.
21st Dec 2017, 11:37 AM
Laura
Laura - avatar
0
alternatively you can probably do something like f=file.open(filename) then (in a loop) wordlist.append(f.readline())
21st Dec 2017, 11:53 AM
ifl
ifl - avatar
0
Is anyone's willing to edit the code in the next 5 minutes, awesome. Otherwise I'm screwed cause deadline in 5 and I'm not getting this.
21st Dec 2017, 11:53 AM
Laura
Laura - avatar
- 1
mmmh... looks like wordlist is still empty when you reach this line?
21st Dec 2017, 11:05 AM
ifl
ifl - avatar
- 2
well, wordlist is defined as an empty array and then there is no code to add values to it... so that would cause an error when you try to read from it, right?
21st Dec 2017, 11:22 AM
ifl
ifl - avatar
- 2
try this wordlist = line.split() to fill up the array....
21st Dec 2017, 11:34 AM
ifl
ifl - avatar