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

question 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):

the code that I wrote is 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

OpenStudy (anonymous):

and the teachers automated thing replied with test_3_load_words ================= Total tests: 4, passed: 1, failed: 3, had errors: 0 SUCCESS: h u1 quellan csc108 exercises e3 repos e3 c2bhatti e3: test_3_load_words.TestCases METHOD: test_1a_return_type Passed FAILURE: 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 27, in test_1b_single_line self.assertEqual(result, expected_result, msg) AssertionError: e3.load_words(test_file) should return {'a': ['apple']}, but returned {}. test_file contains: apple FAILURE: 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 38, in test_1c_multi_line_different_letters self.assertEqual(result, expected_result, msg) AssertionError: e3.load_words(test_file) should return {'a': ['apple'], 'p': ['pear'], 'b': ['banana']}, but returned {}. test_file contains: apple banana pear FAILURE: 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 52, in test_1d_multi_line_duplicate_letters self.assertEqual(result, expected_result, msg) AssertionError: e3.load_words(test_file) should return {'a': ['apple'], 'p': ['peach', 'pear', 'pineapple'], 'b': ['banana', 'blueberry'], 'o': ['orange']}, but returned {}. test_file contains: apple banana pear orange pineapple peach blueberry

OpenStudy (anonymous):

i thought i was write but i dont know (N)

OpenStudy (anonymous):

Is the input an actual file? Because you're going to have to actually open the file and load the information contained within into a variable. Have you been taught about the open() function to open files?

OpenStudy (anonymous):

i think we have been but i dont know how to use it and yeah the input is a file he gives some example the banana blueberry stuff in his markings but i dont know so confused about this

OpenStudy (anonymous):

was i wrong

OpenStudy (anonymous):

Your code doesn't look bad. The first (or second) line of your code should be something like: f = open(file1,'r') What this does is it creates a variable that contains the contents of the file. The 'r' tells the program that we're going to read the file...if we wanted to write to a file, we'd use 'w' instead. Then, instead of using file1 later in the function, you should use f instead (or whatever you choose to call the variable. At the end of the function, it is good practice to call f.close() to close the file. It's not strictly necessary, but it's a good habit.

OpenStudy (anonymous):

is r a builtin function?

OpenStudy (anonymous):

so basically i will have 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):

Yes, but there is one more problem with your code...you're not doing anything if line[0] is not in dict1. Easy fix...take your else clause and align it with the first if, not the second.

OpenStudy (anonymous):

hmm im not sure what you mean by that

OpenStudy (anonymous):

like thiss? 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

OpenStudy (anonymous):

Yes.

OpenStudy (anonymous):

But don't forget the file stuff.

OpenStudy (anonymous):

but you said i should include the close function where would I include it or is it not neccerary

OpenStudy (anonymous):

o snap i dont have it there

OpenStudy (anonymous):

At the end, like you did last time.

OpenStudy (anonymous):

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 theree

OpenStudy (anonymous):

Try it.

OpenStudy (anonymous):

For the record, the description is a bit unclear. If the file looked like this: apple apple apple apple apple ...does the program want you to return {'a':['apple']} or {'a':['apple','apple','apple','apple','apple]}? You currently have your program doing the former, which is probably what I would do, but I don't know if your teacher wants the former or the latter...maybe this is part of a bigger program and that's made clear.

OpenStudy (anonymous):

ooo hmm i think he wants the former thats how it looked like in his automated marking

OpenStudy (anonymous):

Okay, then you're good.

OpenStudy (anonymous):

hey one more question you know the last code i wrode is my FIRST line when i define it is it going to be load_words(file1) or load_words(f)

OpenStudy (anonymous):

load_words(file1) Then the first line of code takes file1 (which is just the name of the file), opens it, and creates a file object called f.

OpenStudy (anonymous):

alright perfect thanks a lot man appreciate itt

OpenStudy (anonymous):

btw want to do my assignment :P

OpenStudy (anonymous):

Well, I *am* taking this summer to get as good at Python as I can possibly be (I'm teaching a high school class on Python for the first time in January). But...no.

OpenStudy (anonymous):

LOL worth a try :P

OpenStudy (anonymous):

where do you currently teachh

OpenStudy (anonymous):

Lincoln, Nebraska (US)

OpenStudy (anonymous):

ooo cool cool goodluck you will make an amazing teacher (Y)

OpenStudy (anonymous):

Well, I already am a teacher, but I finally convinced them to give me a programming class. :)

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!
Latest Questions
Burro: Vc anyone
5 hours ago 4 Replies 0 Medals
chatAI: Happy Easter!
10 hours ago 0 Replies 0 Medals
danielfootball123: HAPPY EASTER!!!!
3 hours ago 23 Replies 2 Medals
ShadowKid3: I drew Leon again (used suggestions from last art post) so Leon v2 :D
10 hours ago 4 Replies 0 Medals
Puck: for all you amazing people
10 hours ago 3 Replies 2 Medals
Breathless: update on drawing.
13 hours ago 12 Replies 5 Medals
ShadowKid3: what are examples of a class three lever?
2 days ago 3 Replies 2 Medals
alphaXtiger: Help pls
2 days ago 3 Replies 1 Medal
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!