Ask your own question, for FREE!
Computer Science 17 Online
kekeman:

During your entry-level job interview, you are tasked with creating a list from the file and appending two lines of text to the end of the list. You wrote the following code that has a bug: def main(): f = open("text.txt","w") myItems = [] for line in content: myItems.append(line) myItems.append("Greetings") myItems.append("Goodbye") print(myItems) f.open() main() Which change should you make to fix the code? On line 3, change "w" to "r". On line 5, add an additional loop. On line 9, delete the word myItems. On line 10, change f.open() to f.close().

kekeman:

For this question I picked On line 5, add an additional loop. I just wanna make sure I am right

SmokeyBrown:

I'm not sure I understand completely what an additional loop would be needed for. I thought that f.open() would be changed to f.close(), since the program would be done writing to the file and would be able to close that resource. Actually, I suspect that the indentation of the code might be a bit off; it seems like only the line "myItems.append(line)" should be indented as part of the for-loop, since that line should occur for each line in the file; the lines after that only occur once each time the program is run, outside of the loop. In any case, I think the correct answer should be "On line 10, change f.open() to f.close()."

kekeman:

Oh I see what you are talking about because the line " f = open("text.txt","w")" is already opening it so then we have to use f.close() to close it

SmokeyBrown:

Yup, exactly

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!