I'm having the hardest time figuring out how my latest homework assignment is supposed to work. Basically we have to write a program that creates a list containing names. The list will then be sorted, written to an output file, and searched. We get the names from a text file that is provided to us. It must contain 4 functions. Here's what I have so far.
def main():
name_list()
print_list()
write_output()
search_list()
def name_list():
infile = open('names.txt', 'r')
names = infile.readlines()
infile.close()
index = 0
while index < len(names):
names[index] = names[index].rstrip('\n')
index += 1
return names
def print_list():
names = [infile]
print 'Original order:', names
names.sort()
print 'Sorted order:', names
def write_output():
outfile = open('names.txt', 'w')
outfile.write(names)
outfile.close()
def search_list():
search = input ('Enter last name, first name:')
if search in text_file:
print
else:
print
main()
After reading the book I feel pretty good about my first function. Although that could be wrong, too. I need to return the list of names to the main function then pass the names_list to the remaining functions. The search_list function needs to say whether the input name is in the list and what its index is in the list.
The problem is I keep getting an error for no such file or directory. Anyone have a clue as to why it wont read the list I was provided. I know the codes a mess but I copied the code from the book by itself to see if it would read it and it still wouldn't work. That's like the first step in the program so until I figure that out I can't proceed.
Thanks for any help you can offer.