#==============================================================================# # Program: Make and display lists, read/write files # Programmer: # Date: 11/06/2013 # Abstract: Displays the list of names in a file, sorts them, reprints them, # adds these to a new file. Allows a user to search the list for # a specific name. #==============================================================================# #defines what order to perform the functions, and sorts the list once created. def main(): nameList = makeList() printList(nameList) nameList.sort() printList(nameList) newOutput(nameList) searchNames(nameList) #creates the list from a text file def makeList(): data = open('names.txt', 'r') l = data.readlines() data.close() return l #prints the list to the screen def printList(nameList): for n in nameList: print(n) #creates a new output file, based on the first but in sorted order. def newOutput(nameList): newList = open('newOutput.txt', 'w') newList.writelines(nameList) newList.close() #search the list for a specific name. Includes a loop to allow a user to search multiple names. def searchNames(nameList): another = 'y' while another.lower() == 'y': search = input("What name are you looking for? (Use 'Lastname, Firstname', including comma: ") if search in nameList: print("The name was found at index", nameList.index(search), "in the list.") another = input("Check another name? Y for yes, anything else for no: ") else: print("The name was not found in the list.") another = input("Check another name? Y for yes, anything else for no: ") #Call main function main()