38.9k views
0 votes
Write a program in Python. Your program needs to allow the user to create a shopping list from scratch, or read a shopping list from an external file.

The initial concept should be to ask the user if they wish to read the list, or create a list.
If create a list option is chosen, you should then allow them to enter in all the items into the list and have them write those items to a file. Once the file is generated you should be able to print the items to the screen to show that the items were stored successfully to your file.

User Blkedy
by
7.3k points

1 Answer

4 votes

Answer: Your welcome!

Step-by-step explanation:

If read a list is chosen, you should then allow the user to enter in the file name they wish to read and print the items contained in that file to the screen.

# Program to Create/Read Shopping List

# Create an empty list to store items

shopping_list = []

# Ask the user if they wish to read a list or create a list

action = input("Do you wish to create a list or read a list? (create/read): ")

# Check the user's input

if action.lower() == "create":

item = input("Enter an item to add to your list (enter 'done' when finished): ")

# Loop until the user enters 'done'

while item.lower() != "done":

# Add the item to the shopping list

shopping_list.append(item)

# Ask the user for the next item

item = input("Enter an item to add to your list (enter 'done' when finished): ")

# Open a file to write the list to

file = open("shopping_list.txt","w")

# Loop through the list and write each item to the file

for item in shopping_list:

file.write(item + "\\")

# Close the file

file.close()

print("Your shopping list has been saved.")

print("Items in your list:")

# Print the list to the screen

for item in shopping_list:

print(item)

elif action.lower() == "read":

# Ask the user for the file name

filename = input("Enter the file name: ")

# Open the file

file = open(filename,"r")

# Read the contents of the file

contents = file.read()

# Close the file

file.close()

print("Items in your list:")

# Print the contents of the file to the screen

print(contents)

else:

print("Invalid input.")

User Yoonjesung
by
7.6k points