KS4 Puzzle 10 – import from text file

Code / algorithm aim

The program should declare a list of fruit then display everything in the list on the screen.

The program should then open a text file called fruits and import the lines of text into the list printing each line of text as it imports it.

The program should close the text file then display to the user the new list with the added items.


How does the puzzle work?

Simply drag the code blocks from the left to the right and put them in the correct order.

Remember that some blocks will need to be indented just like in Python – to indent the block simply drag the block further to the right of the code above

If you think you have the order right click on the “Check your answer” button.

If you didn’t get it right you will be provided with some hints so you can try again.

To close this prompt click on it or press the Instructions button again

Well done! You did it in 0 attempt(s)

Check out and run the code in repl: Parsons Puzzle code

What does the code do?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
fruits =["apple","orange","cherry"]
for i in fruits:
  print(i)
file=open("fruits.txt","r")
for j in file:
  j = j.rstrip('\n')
  print(j)
  fruits.append(j)
file.close()
for i in fruits:
  print(i)

Line 1 defines our list called fruits. We declare 3 items in our list apple, orange, cherry.

Line 2 is looking for all the items we have in our list. Every time it finds a value it is storing it as a temporary variable called “i” then goes to the next instruction. If it cannot find any more values in our list it will stop the for loop.

Line 3 is displaying whatever value has been found in our list which has been saved to the temporary value of “i”. The for loop will continue displaying every item in our list until it gets to the end and then the for loop will end.

Line 4 is opening a text file called fruits.txt and assigning it to a variable called file. The “r” indicates that the file can only be read.

Line 5 is looking for all the lines of text we have in our file. Every time it finds a line it is storing it as a temporary variable called “j” then goes to the next instruction. If it cannot find any more values in our list it will stop the for loop.

Line 6 is stripping out (getting rid of) the line breaks in the .txt file. Even though we cannot see them in the file every time there is a new line there is code that looks like this “\n”. If we do not get rid of this we will import it.

Line 7 displays the line of text that our code has extracted from the text file and put into the temporary variable j.

Line 8 adds whatever is saved in temporary variable j to the end of our fruits list.

Line 9 closes the text file now that we have read from it. It is always important to close the file once we are done.

Line 10 is looking for all the items we have in our list. Every time it finds a value it is storing it as a temporary variable called “i” then goes to the next instruction. If it cannot find any more values in our list it will stop the for loop.

Line 11 is displaying whatever value has been found in our list which has been saved to the temporary value of “i”. The for loop will continue displaying every item in our list until it gets to the end and then the for loop will end.

You have tried this : 0 time(s)

Finished KS4 Puzzle 10? Go back and choose another Parsons puzzle.

This code is based on js-parsons available at https://github.com/js-parsons/js-parsons and distributed under an MIT license