+ 1

Help with an assignment? Finding words in a grid.

I'm very very new to python, just a heads-up. I need help with an assignment where I'm supposed to find selected words from a grid of random letters. I have no idea where to start. I don't know how to approach this. Uni doesn't help much, just lets us wander in the dark alone, without guidance. So I have to resort to internet for some kind of pointers. This is the full description: Write code to prompt the user for a filename, and attempt to open the file whose name is supplied. If the file cannot be opened the user should be asked to supply another filename; this should continue until a file has been successfully opened. The file will contain on each line a row from the grid. For example: xmmycxvtljlqbbybkoumjqwbtbufve buubmekxbeydqmcnzyjpvdackomdmi Write code to read, in turn, each line of the file, remove the newline character and append the resulting string to a list of strings. After the input is complete the strings in the list should be displayed on the screen, one per line. Note that grids used for testing the submitted programs may have different widths and heights from the sample one that will be provided. Grids will contain only lower-case letters. Write a function that will take two arguments: a word to be searched for and a list of strings containing the grid. The function should search for a horizontal (left-to-right or right-to-left) or vertical (downwards or upwards) occurrence of the word. If a word occurs more than once the function is required to find only one occurrence so you should stop searching once the word has been found. My attempts so far: https://code.sololearn.com/c9o7lbD8xSaK/#py Further on I don't know how to actually search within the list. Any pointers on how to do that? Do I have to create while loops? How do I do that in this particular case? I'm not sure how I'm supposed to structure it. I barely know the basics of python and have to handle this assignment. So any advice and explanation would be greatly appreciated.

20th Dec 2017, 6:53 AM
Laura
Laura - avatar
12 Respostas
+ 1
I uploaded it to my page sorry for not being clear enough. #this is a loop to check if the file is present in the current directory def gettingfile(): try: global files files = input('File name:') with open (files + '.txt',) as opened: opened.close() pass except IOError: print('file is not present in current directory') gettingfile() #this is just to check if file is present. if the file is present in current directory it will completely skip except and follow up down here with open(files + ‘.txt’) as o: for x in o.readlines(): temp = x.strip() print(temp) Now all you need to do is go learn about re and how it works and you can probably figure out what to do next (: Hopefully this is what you are looking for, this is all I can help with (:
21st Dec 2017, 8:06 AM
Sean Cedano
Sean Cedano - avatar
+ 1
Incorrect, where are you placing this? Make sure you call gettingfile() before placing your code. It’ll look something like this. gettingfile() wordlist = [] with open(files + ‘.txt’, ‘a’) as line: for line in wordlist: line.write(wordlist.split()) Also you’ll need to open the file to write. Using ‘a’ or ‘w’ instead of ‘r’ w wipes the file before writing to it a appends leaving all previous content .strip() removes \n
21st Dec 2017, 8:32 AM
Sean Cedano
Sean Cedano - avatar
+ 1
Oops made a mistake remove line it should just be x.strip()
21st Dec 2017, 8:44 AM
Sean Cedano
Sean Cedano - avatar
+ 1
Holy mother of Jesus Christ and all that is holy! It prints the grid! Thank you thank you thank you!!
21st Dec 2017, 8:46 AM
Laura
Laura - avatar
0
To search within a list use import re There’s lesson on SoloLearn that covers a portion of how it works. I’ll be editing your attempt shortly with a working example (:
20th Dec 2017, 8:10 PM
Sean Cedano
Sean Cedano - avatar
0
Nothing?
21st Dec 2017, 6:55 AM
Laura
Laura - avatar
0
Was testing that now and it's not actually letting me edit it further when I try to type code to append the found file lines to a list. Error: for line in files: NameError: name 'files' is not defined
21st Dec 2017, 8:24 AM
Laura
Laura - avatar
0
Make sure you include global at the top where I did. That’ll let you use it outside of the function
21st Dec 2017, 8:26 AM
Sean Cedano
Sean Cedano - avatar
0
for line in global files: wordlist.append(line.split()) as such?
21st Dec 2017, 8:28 AM
Laura
Laura - avatar
0
temp = x.line.strip() AttributeError: 'str' object has no attribute 'line'
21st Dec 2017, 8:42 AM
Laura
Laura - avatar
0
No problem :) Have a great day
21st Dec 2017, 8:47 AM
Sean Cedano
Sean Cedano - avatar
0
If you're still around, mind checking out this code: https://code.sololearn.com/cJ6Mf69XnMF8/#py It doesn't work past reading the file and putting it in list. Error I'm getting is: ValueError: '\n' is not in list
21st Dec 2017, 11:22 AM
Laura
Laura - avatar