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.

Cookies

⭐⭐

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.

Success Criteria

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

  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.
  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

Restricted automated feedback

Automated feedback for this assignment is still under construction. Submitted programs are checked for syntax errors and their source code is checked for potential errors, bugs, stylistic issues, and suspicious constructs. However, no checks are performed yet to see if the program correctly implements the behaviour specified in the assignment.