Question: Python program to remember a list's contents and print items each time it is run. I'm trying to write a program in python that allows
Python program to remember a list's contents and print items each time it is run.
I'm trying to write a program in python that allows a user to enter 1 color and add it to a list, every time the program is run. No duplicates colors should be allowed, eg if the first time the program is run a user adds "red", then "red" cannot be added again the next times the program is run.
I'm trying to make it so every execution, the color is saved to the list and the program remembers it and prints a list of all the colors in the list after a user enters their color.
I'm having a few problems with this code where
1. the program is allowing duplicates
2. the colors are not being saved as separate list items
Here is my code so far:
list = []
while True: color = input("add a color to the list: ").lower().strip() if color not in list: list.append(color) break else: print("color already exists, try again") continue
temp = " ".join(list)
with open("colorsfile", "a") as f: f.write(temp)
with open("colorsfile") as g: mylist = g.read().splitlines()
print(mylist)
Any help would be much appreciated
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
