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!
Latest Questions
Breathless: Spooky witch but cute
4 hours ago 3 Replies 0 Medals
Arriyanalol: help
4 hours ago 10 Replies 2 Medals
Arriyanalol: @tinydinoUwU stop trying to find a argument u blad lil boy
1 day ago 5 Replies 4 Medals
Jaded012023: Please tell me what you all think of this song
7 hours ago 6 Replies 1 Medal
Arriyanalol: bro how
7 hours ago 2 Replies 3 Medals
Arriyanalol: cant wait for the new bluey movie in 2027
1 day ago 12 Replies 2 Medals
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!