Try it:

Use and store data in files
Writing data to a file in python

Below you’ll see some example python code writes to a file called instructions.txt

Press Ctrl + Enter  to run the code.

The "w" on line 4 means that the file instructions.txt is opened in write mode which means that anything already in that file will be replaced by whatever you write to it.

The "a" on line 8 means that the file is now opened in append mode which means you don’t lose anything you’ve already written to that file.

Challenges:

Start by running the code.

  1. Write the line I will always submit my homework on time to the file lines.txt

    You’ll need to change the filename to lines.txt

  2. Write the line I will always submit my homework on time 10 times to the file lines.txt

    You could use a for loop to write the same line 10 times

    Don’t forget to add a \n character on the end of each line otherwise all the text will be all on the same line.

  3. Append the word DONE on to the end of the the file todo.txt

    You will need to open todo.txt in append mode ("a") so that you don’t overwrite what’s already in that file.

On the next page you’ll get some code with both syntax and logic errors

KPRIDE

KPRIDE stands for Keywords, Predict, Run, Investigate, Debug and Extend and it’s a way of helping you explore and understand python code. Click on the image below for a set of KPRIDE activities for this python skill.

Page 1: Intro

Page 2: The theory: learn what you need to know as fast as possible.

Page 3: Try it: try out and adapt some working python code snippets.

Page 4: Debug it: Learn how to find and fix common mistakes.

Page 5: Extend it: Choose a project idea to use your newfound python skills.