Advertisement
Guest User

Untitled

a guest
Nov 6th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. #==============================================================================#
  2. # Program: Make and display lists, read/write files
  3. # Programmer:   asdasda
  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 = 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.     list = data.readlines()
  23.     data.close()
  24.     return list
  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.     for n in nameList:
  35.         newList.write(n)
  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.         for n in nameList:
  44.             result = nameList[n].rstrip('\n')
  45.             if search == result:
  46.                 print("The namewas found at index", n, "in the list.")
  47.                 another = input("Check another name? Y for yes, anything else for no.")
  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
Advertisement