13: Writing data to a file in python

Debug it

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

Below is some code that has been deliberately broken so that you can fix it. 

The code is supposed to ask any number of people what type of takeaway they would like to order and save their choices in a CSV file:


  1. Fix all syntax errors

    Hint: A syntax error means the code has broken the rules of the language. Work upwards from the line number in the error message to see what’s wrong or what’s missing.

  2. Fix the runtime error so that the file opens in write mode

    Hint: write mode is "w"

  3. Fix the logic error so all choices appear on separate lines

    Hint: A CSV file is a Comma Separated Value file which means there’s a , between each value and a \n newline character after each group of values

On the next page you’ll get some ideas for projects where you can use file output to create your own projects

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.

Leave a Reply