Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==============================================================================#
- # Program: Make and display lists, read/write files
- # Programmer: asdasda
- # 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 = nameList.sort()
- printList(nameList)
- newOutput(nameList)
- searchNames(nameList)
- #creates the list from a text file
- def makeList():
- data = open('names.txt', 'r')
- list = data.readlines()
- data.close()
- return list
- #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')
- for n in nameList:
- newList.write(n)
- #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.: ")
- for n in nameList:
- result = nameList[n].rstrip('\n')
- if search == result:
- print("The namewas found at index", n, "in the list.")
- another = input("Check another name? Y for yes, anything else for no.")
- 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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement