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.
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.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
Complete the subprogram called read_cookie that:
preferences.txt for reading data. You can assume this file exists.theme = from the data.Complete the subprogram called change_theme that:
write_cookie subprogram to write the new theme to the file.Complete the subprogram called write_cookie that:
preferences.txt for writing data.Complete the main program so that:
read_cookie function is called to return the current theme.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
# Cookies program
# -------------------------
# Subprograms
# -------------------------
# Read the light/dark theme from the cookie file
---
def read_cookie():
---
file = open("preferences.txt", "r")
---
theme = file.readline()
---
theme = theme.strip()
---
file.close()
---
return theme
---
# Write the new theme to the cookie file
def write_cookie(theme):
---
file = open("preferences.txt", "w")
---
theme = theme + "\n"
---
file.write(theme)
---
file.close()
---
# Swap the theme
def change_theme(theme):
---
# Dark to light or light to dark
if theme == "theme = dark":
---
theme = "theme = light"
---
else:
---
theme = "theme = dark"
---
write_cookie(theme)
# -------------------------
# Main program
# -------------------------
---
theme = read_cookie()
---
print("The current", theme)
---
change = input("Would you like to change it? y/n: ")
---
# Swap the theme if the user enters y/Y
if change.lower() == "y":
---
change_theme(theme)