Cookies

Cookies

Saving website preferences.

Websites save user preferences in small files called cookies. This allows the user to customise their experience on the website with preferences saved for when they return to the site in the future.

Make


Write a program that reads a text file stored on the computer and outputs the contents. This will either be: theme = dark or theme = light. The program then asks the user if they wish to change the theme, and if they do writes this new data to the file, overwriting what was previously stored.

Use this boilerplate code as a starting point:

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called `read_cookie` that:

  1. Opens a file called `preferences.txt` for reading data. You can assume this file exists.
  2. Reads the current theme from the file.
  3. Returns the theme. There is no requirement to split `theme =` from the data.

Complete the subprogram called `change_theme` that:

  1. Takes a parameter that is the theme.
  2. If the theme is dark it is swapped to light and vice versa.
  3. Calls the `write_cookie` subprogram to write the new theme to the file.

Complete the subprogram called `write_cookie` that:

  1. Takes a parameter that is the theme.
  2. Opens a file called `preferences.txt` for writing data.
  3. Writes the theme to the file.

Complete the `main program` so that:

  1. The `read_cookie` function is called to return the current theme.
  2. Outputs the current theme.
  3. Asks the user if they want to swap the theme.
  4. If the user answers "y" or "Y" the `change_theme` subprogram is called.

Typical inputs and outputs from the program would be:

The current theme = dark

Would you like to change it? y/n: n


The current theme = dark

Would you like to change it? y/n: y

The structure of the preferences.txt file is:

theme = dark

or...

theme = light

Knowledge Organiser

Use these resources as a reference to help you meet the success criteria.

Programming guide:

Evaluate


Run the program several times and open the `preferences.txt` file afterwards to check that your program has met the success criteria.

Check that you have:

  • Used comments within the code to describe the purpose of subprograms, conditions and iterations.
  • Used meaningful identifier names. That means the names of subprograms and variables indicate what they are for.

Registered in England and Wales: 10442992

VAT Number: 290 9845 58

Telephone: 01452 947500

Email: admin@craigndave.co.uk

Craig'n'Dave logo

Craig 'n' Dave

Bett Awards 2024 Finalist