— getList(filename), returns a list of strings —
# (open/create file and return lines of text as a list of strings) def getList(filename): try: with open(filename, ‘r’) as file: data = file.readlines() data = [line.strip() for line in data] except FileNotFoundError: with open(filename, ‘w’) as file: data = [] return data
— showList(data), returns nothing —
# (receive list of strings and display them, or “nothing in list” message) def showList(data): if len(data) == 0: print(‘Nothing in list.’) else: for i, item in enumerate(data): print(f’{i+1}. {item}’)
— addToList(filename, data), returns a list of strings —
# (prompt for an item to add to the list of strings and append to the file) def addToList(filename, data): item = input(‘Enter item to add: ‘) data.append(item) with open(filename, ‘a’) as file: file.write(f’{item}\n’) return data
— deleteFromList(filename, data), returns a list of strings —
# (prompt for item number to delete from the list of strings and write list to the file) def deleteFromList(filename, data): showList(data) item_num = input(‘Enter item number to delete: ‘) try: item_num = int(item_num) if item_num < 1 or item_num > len(data): print(‘Invalid item number.’) else: item = data.pop(item_num-1) with open(filename, ‘w’) as file: for line in data: file.write(f’{line}\n’) print(f’{item} deleted.’) except ValueError: print(‘Invalid item number.’) return data
— main part of program —
FILENAME = ‘list.txt’ # define the filename used to store the list lineList = getList(FILENAME) # call the getList function to read the file into a list
while True: # this endless loop displays the list and prompts the user for a command showList(lineList) # call showList to show the current content of the list # show the instructions for the possible commands - [a]dd, [d]elete, e[x]it print(‘\nType “a” to add an item.’) if len(lineList) > 0: # only show the delete instruction if the list has items print(‘Type “d” to delete an item.’) print(‘Type “x” to exit.’) command = input(‘> ‘) # prompt for a command # if “a”, calladdToList to prompt for item and add to list if command == ‘a’: lineList = addToList(FILENAME, lineList) # if “d”, call deleteFromList to prompt for item number and delete from list elif command == ‘d’ and len(lineList) > 0: lineList = deleteFromList(FILENAME, lineList) elif command == ‘x’: # if “x”, break out of the loop to end the program print(‘Goodbye!’) break else: # if anything else, show an error message print(‘Invalid command.\n’)
Loading...