Dec-22-2017, 01:00 PM
Supposed to ask the user to input the file name, then read and append each line from the file to a list and display the grid. Then find some words in the puzzle grid. Actual assignment was to ask the user to supply the name of the file which contains the words to be found, but didn't even bother with that cause the rest was complicated enough.
import io import array import re solutions = ['java', 'ethernet', 'stock', 'active', 'act', 'cot', 'net', 'nut', 'caps', 'lap', 'notinthegrid'] def gettingfile(): try: global files files = input('Enter the file name: ') with open (files + '.txt',) as opened: opened.readlines() opened.close() pass except IOError: print('Invalid file name. Try again.') gettingfile() with open(files + '.txt') as o: for x in o.readlines(): temp = x.strip() print(temp) gettingfile() wordlist=[] with open(files + '.txt', 'r') as line: for line in line: wordlist.append(line.split()) with open(files + '.txt', 'a') as derp: for derp in wordlist: derp.write(wordlist.strip('\n')) length = wordlist.index('\n')+1 characters = [(letter, divmod(index, length)) for index, letter in enumerate (wordlist)] wordlines = {} directions = {'going downwards':0, 'going downwards and left diagonally':-1, 'going downwards and right diagonally':1} for word_direction, directions in directions.items(): wordlines[word_direction] = [] for x in range(length): for i in range(x, len(characters), length + directions): wordlines[word_direction].append(characters[i]) wordlines[word_direction].append('\n') wordlines['going right'] = characters wordlines['going left'] = [i for i in reversed(characters)] wordlines['going upwards'] = [i for i in reversed(wordlines['going downwards'])] wordlines['going upwards and left diagonally'] = [i for i in reversed(wordlines['going downwards and right diagonally'])] wordlines['going upwards and right diagonally'] = [i for i in reversed(wordlines['going downwards and left diagonally'])] def printitout(direction, tuple, lines): print ("Keep in mind, rows are horizontal and columns are vertical.\n") for direction, tuple in lines.items(): string = ''.join([i[0] for i in tuple]) for word in solutions: if word in string: coordinates = tuple[string.index(word)][1] print (word, 'is at row', coordinates[0]+1, 'and column', coordinates[1]+1, direction + ".") printitout(word_direction, tuple, wordlines)Error:
Error:derp.write(wordlist.strip('\n'))
AttributeError: 'list' object has no attribute 'write'
Initially the code didn't include the part that gives error. I was getting another error and was advised to strip the line/list? Confused. So I typed that. But initial code was:import io import array import re solutions = ['java', 'ethernet', 'stock', 'active', 'act', 'cot', 'net', 'nut', 'caps', 'lap', 'notinthegrid'] def gettingfile(): try: global files files = input('Enter the file name: ') with open (files + '.txt',) as opened: opened.readlines() opened.close() pass except IOError: print('Invalid file name. Try again.') gettingfile() with open(files + '.txt') as o: for x in o.readlines(): temp = x.strip() print(temp) gettingfile() wordlist=[] with open(files + '.txt', 'r') as line: for line in line: wordlist.append(line.split()) length = wordlist.index('\n')+1 characters = [(letter, divmod(index, length)) for index, letter in enumerate (wordlist)] wordlines = {} directions = {'going downwards':0, 'going downwards and left diagonally':-1, 'going downwards and right diagonally':1} for word_direction, directions in directions.items(): wordlines[word_direction] = [] for x in range(length): for i in range(x, len(characters), length + directions): wordlines[word_direction].append(characters[i]) wordlines[word_direction].append('\n') wordlines['going right'] = characters wordlines['going left'] = [i for i in reversed(characters)] wordlines['going upwards'] = [i for i in reversed(wordlines['going downwards'])] wordlines['going upwards and left diagonally'] = [i for i in reversed(wordlines['going downwards and right diagonally'])] wordlines['going upwards and right diagonally'] = [i for i in reversed(wordlines['going downwards and left diagonally'])] def printitout(direction, tuple, lines): print ("Keep in mind, rows are horizontal and columns are vertical.\n") for direction, tuple in lines.items(): string = ''.join([i[0] for i in tuple]) for word in solutions: if word in string: coordinates = tuple[string.index(word)][1] print (word, 'is at row', coordinates[0]+1, 'and column', coordinates[1]+1, direction + ".") printitout(word_direction, tuple, wordlines)Which gave this error:
Error:length = wordlist.index('\n')+1
ValueError: '\n' is not in list