hey guyz,
i need to make a code for searching the names in the list txt. i pretty much got the other parts, but when im kinda stuck on searching.

def main():
    list_of_names = open_file()
    print"""
--------------- non sorted---------------------"""
    print_names(list_of_names)
    print """
----------------sorted-------------------------"""
    list_of_names = sort_names(list_of_names)
    save_file(list_of_names)
    search_names(list_of_names)

def open_file():
    list_of_names = []
    name = open('names.txt','r')
    s_names = name.readline()
    while s_names != '':
        s_names = s_names.rstrip('\n')
        list_of_names.append(s_names)
        s_names = name.readline()
    return list_of_names
        
def sort_names(list_of_names):
    list_of_names.sort()
    print_names(list_of_names)
    return list_of_names

    
def save_file(list_of_names):
    Name = open('namestwo.txt','w')
    for name in list_of_names:
        Name.write(name + '\n')

def search_names(list_of_names):
    search = input('Enter search name: ')
    for search in list_of_names:
        print search

def print_names(list_of_names):
    for name in list_of_names:
        print name
main()

Just little errors, first need to use raw_input() when inputting strings, and have to use a different variable instead of search in the for loop.

def main():
    list_of_names = open_file()
    print"""
--------------- non sorted---------------------"""
    print_names(list_of_names)
    print """
----------------sorted-------------------------"""
    list_of_names = sort_names(list_of_names)
    save_file(list_of_names)
    search_names(list_of_names)

def open_file():
    list_of_names = []
    name = open('names.txt','r')
    s_names = name.readline()
    while s_names != '':
        s_names = s_names.rstrip('\n')
        list_of_names.append(s_names)
        s_names = name.readline()
    return list_of_names
        
def sort_names(list_of_names):
    list_of_names.sort()
    print_names(list_of_names)
    return list_of_names

    
def save_file(list_of_names):
    Name = open('namestwo.txt','w')
    for name in list_of_names:
        Name.write(name + '\n')

def search_names(list_of_names):
    search = raw_input('Enter search name: ')
    for nameSearch in list_of_names:
        if search == nameSearch:
            print search

def print_names(list_of_names):
    for name in list_of_names:
        print name
        
if __name__ == '__main__': main()
commented: very helpful +10

Also, when you open a file for reading or writing, it is a good idea to close it when you are done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.