Ask your own question, for FREE!
Computer Science 12 Online
OpenStudy (anonymous):

question 2 The parameter name is "load_words: (file)" and the output expected is "dict of {str: list of strs}". The description of the problem given is: "The open file contains one lowercase word per line. Return a dictionary in which each key is a single lowercase letter and each value is a list of the words from the file that start with that letter. Only letters that one or more words from the file start with appear as keys in the dictionary. "

OpenStudy (anonymous):

@shandelman

OpenStudy (anonymous):

heyy good morning, you know the code that we did for this one that i submitted was def load_words(file1): f = open(file1, 'r') dict1 = {} for line in f: line = line.strip() if line[0] in dict1: if line not in dict1[line[0]]: dict1[line[0]].append(line) else: dict1[line[0]] = [line] f.close() return dict1

OpenStudy (anonymous):

but the teachers online thing gave me 3 errors and it replied with test_3_load_words ================= Total tests: 4, passed: 0, failed: 0, had errors: 4 ERROR: h u1 quellan csc108 exercises e3 repos e3 c2bhatti e3: test_3_load_words.TestCases METHOD: test_1a_return_type Traceback (most recent call last): File "/h/u1/quellan/csc108/exercises/e3/tests/test_3_load_words.py", line 12, in test_1a_return_type result = e3.load_words(argument) File "/h/u1/quellan/csc108/exercises/e3/repos/e3/c2bhatti/e3/e3.py", line 21, in load_words f = open(file1, 'r') TypeError: coercing to Unicode: need string or buffer, file found ERROR: h u1 quellan csc108 exercises e3 repos e3 c2bhatti e3: test_3_load_words.TestCases METHOD: test_1b_single_line Traceback (most recent call last): File "/h/u1/quellan/csc108/exercises/e3/tests/test_3_load_words.py", line 22, in test_1b_single_line result = e3.load_words(argument) File "/h/u1/quellan/csc108/exercises/e3/repos/e3/c2bhatti/e3/e3.py", line 21, in load_words f = open(file1, 'r') TypeError: coercing to Unicode: need string or buffer, file found ERROR: h u1 quellan csc108 exercises e3 repos e3 c2bhatti e3: test_3_load_words.TestCases METHOD: test_1c_multi_line_different_letters Traceback (most recent call last): File "/h/u1/quellan/csc108/exercises/e3/tests/test_3_load_words.py", line 33, in test_1c_multi_line_different_letters result = e3.load_words(argument) File "/h/u1/quellan/csc108/exercises/e3/repos/e3/c2bhatti/e3/e3.py", line 21, in load_words f = open(file1, 'r') TypeError: coercing to Unicode: need string or buffer, file found ERROR: h u1 quellan csc108 exercises e3 repos e3 c2bhatti e3: test_3_load_words.TestCases METHOD: test_1d_multi_line_duplicate_letters Traceback (most recent call last): File "/h/u1/quellan/csc108/exercises/e3/tests/test_3_load_words.py", line 44, in test_1d_multi_line_duplicate_letters result = e3.load_words(argument) File "/h/u1/quellan/csc108/exercises/e3/repos/e3/c2bhatti/e3/e3.py", line 21, in load_words f = open(file1, 'r') TypeError: coercing to Unicode: need string or buffer, file found

OpenStudy (anonymous):

i dont know what that means, does it mean dont add the f = open(file, 'r') ? helpp

OpenStudy (anonymous):

Hmmm...okay, try this: in that line you mentioned in your last post, change the 'r' to 'rb'. I think it wants to be in binary mode. I may be wrong, but I think that should fix it.

OpenStudy (anonymous):

Actually...hmmm...I take that back. It's very hard to tell what the professor wants if he doesn't give you an example. I thought he wanted a file as input, but maybe he wants a file *object* as input. Try commenting out the file open and close lines and changing f back to file1 and see if it works.

OpenStudy (anonymous):

And if it does, I would e-mail the professor and let him know that the assignment should not say load_words(file), it should say load_words(file object)

OpenStudy (anonymous):

@shandelman so i was checking someone had this same problem but i dont know what exactly he means

OpenStudy (anonymous):

ths is what he said "My definition you just pass it a string name of the file and it opens the file if it is in the same directory, as the python script. Is he passing the opened file as a variable? i.e a=open(words.txt, 'r')

OpenStudy (anonymous):

like exactly what we did but we defined it as f = open(file1, 'r') so this instructor replied with "I noticed this issue in the original E3 posting. I would guess that your assumption is correct., as it is in the line with the excersice guidelines ( It does mention passing in a file, not a string). The script bug may have made what you did looklike it was correct"

OpenStudy (anonymous):

and then that guy replied with "Ok I resubmitted by opening the file and saving it into a variable and passing that into the load_words function and that works" so i dont know what this guy did by introcuing a variable?

OpenStudy (anonymous):

If I understood all that, I'm guessing you have to take out the file open and close lines, then.

OpenStudy (anonymous):

how do i do that?

OpenStudy (anonymous):

Take out the second line, the second to last line, and change the f in the fourth line back to file1.

OpenStudy (anonymous):

def load_words(file1): dict1 = {} for line in file1: line = line.strip() if line[0] in dict1: if line not in dict1[line[0]]: dict1[line[0]].append(line) else: dict1[line[0]] = [line] return dict1 like this?

OpenStudy (anonymous):

so basically my original code then

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!