Guest User

Program

a guest
Nov 12th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. #==============================================================================#
  2. # Program: Make and display lists, read/write files
  3. # Programmer:  
  4. # Date:         11/06/2013
  5. # Abstract:     Displays the list of names in a file, sorts them, reprints them,
  6. #               adds these to a new file. Allows a user to search the list for
  7. #               a specific name.
  8. #==============================================================================#
  9.  
  10. #defines what order to perform the functions, and sorts the list once created.
  11. def main():
  12.     nameList = makeList()
  13.     printList(nameList)
  14.     nameList.sort()
  15.     printList(nameList)
  16.     newOutput(nameList)
  17.     searchNames(nameList)
  18.  
  19. #creates the list from a text file
  20. def makeList():
  21.     data = open('names.txt', 'r')
  22.     l = data.readlines()
  23.     data.close()
  24.     return l
  25.    
  26. #prints the list to the screen
  27. def printList(nameList):
  28.     for n in nameList:
  29.         print(n)
  30.  
  31. #creates a new output file, based on the first but in sorted order.
  32. def newOutput(nameList):
  33.     newList = open('newOutput.txt', 'w')
  34.     newList.writelines(nameList)
  35.     newList.close()
  36.        
  37.  
  38. #search the list for a specific name. Includes a loop to allow a user to search multiple names.
  39. def searchNames(nameList):
  40.     another = 'y'
  41.     while another.lower() == 'y':
  42.         search = input("What name are you looking for? (Use 'Lastname, Firstname', including comma: ")
  43.        
  44.         if search in nameList:
  45.             print("The name was found at index", nameList.index(search), "in the list.")
  46.             another = input("Check another name? Y for yes, anything else for no: ")
  47.         else:
  48.             print("The name was not found in the list.")
  49.             another = input("Check another name? Y for yes, anything else for no: ")
  50.  
  51. #Call main function
  52. main()
Advertisement
Add Comment
Please, Sign In to add comment