0

I have been working on this python issue for a while. I am in an intro level class and I am soooo stuck. Right now I am getting no errors but the program is not printing the data (from names.txt) or prompting me to search. any help would be appreciated. -thanks!

def main():

    print("Last, \tFirst")
    print

    name_list = get_names()
    print_list(name_list)
    new_file(name_list)
    search_list(name_list)

def get_names():
    # open the data file
    infile = open('names.txt', 'r')

    # read all the lines from the file
    name_list = infile.read().split('\n')

    #close the input file
    infile.close()



    return(name_list)

#def print list
def print_list(name_list):
    #print the data
    for name in name_list:
        print (name)
    return(name)

#def new_file
def new_file(name_list):
    outfile = open('sorted_names.txt' ,'w')
    for item in name_list:
        outfile.write(item + '\n')

    outfile.close()

#def search_list
def search_list(name_list):
    again = 'Y'
    while again == 'Y':
        name = input("What name would you like to look for? :")
        try:
            name_index = name_list.index(name)
            print (name), (" was found in the list at index point: "), name_index

        except ValueError as err:
            print (name), (" was not found in the list.")

            print ("Would you like to search for another name?")
            again = input("Would you like to run the program again? [y/n]") == 'y'


# execute the main function
main()
Stephen.J7
  • 55
  • 2
  • 3
  • 10
  • A function needs to be called for it to run. You might want to check this question out: http://stackoverflow.com/questions/419163/what-does-if-name-main-do – monkut Nov 20 '12 at 02:38

2 Answers2

0

main() doesn't do much, it just prints a couple of lines. You will want to make it call your other functions.

Tim
  • 11,710
  • 4
  • 42
  • 43
  • oh wow... well that's some process then! I updated my previous post with my code now. I am getting a syntax error at line 39 now. while index < len(name_list): name_list[index] = name_list[index].rstrip('\n') index += 1 return(name_list) – Stephen.J7 Nov 20 '12 at 03:01
  • line 38 `while index < len(name_list): name_list[index] = name_list[index].rstrip('\n') index += 1 return(name_list)` – Stephen.J7 Nov 20 '12 at 03:06
  • `name_list = infile.read()` means that `name_list` is a string, not a list. If you want it to be a list you should use `.readlines()` instead of `read()`. The loop at line 38 should then work. – Tim Nov 20 '12 at 03:23
0

Corrected version with minimum changes:

def main():

    print("Last, \tFirst")
    print

    name_list = get_names()
    print_list(name_list)
    new_file(name_list)
    search_list(name_list)

def get_names():
    # open the data file
    infile = open('names.txt', 'r')

    # read all the lines from the file
    name_list = infile.read().split('\n')

    #close the input file
    infile.close()

    #print data read into memory
    print(name_list)

    return(name_list)

#def print list
def print_list(name_list):
    #print the data
    for name in name_list:
        print (name)
    return(name)

#def new_file
def new_file(name_list):
    outfile = open('sorted_names.txt' ,'w')
    for item in name_list:
        outfile.write(item + '\n')

    outfile.close()

#def search_list
def search_list(name_list):
    again = 'Y'
    while again.upper()== 'Y':
        name = raw_input("What name would you like to look for? :")
        try:
            name_index = name_list.index(name)
            print (name), (" was found in the list at index point: "), name_index

        except ValueError as err:
            print (name), (" was not found in the list.")

            print ("Would you like to search for another name?")
            again = raw_input("Would you like to run the program again? [y/n]") == 'y'


# execute the main function
main()

What changed:

  • in get_names, the name_list read was actually a string, not a list. To get the list of lines you need to split on the newlines (.split('\n')). After that you don't need to remove the newlines from the names

  • after get_names() returns, you need to store the list and pass to the other functions

  • new_file used name_list, but didn't receive it as argument

  • the try/except block should be nested in the while block

Congrats, it seems to be working (:

BoppreH
  • 8,014
  • 4
  • 34
  • 71
  • THANKS! it seems really close now but I am still seeing a couple problems. on line 44 I am getting an error "cannot determine type". Also the first line printer after Last, First is a list of the names going horizontally . .['Riggs, Jerry', 'Stone, Ruby', 'Wood, Holly',.... ect... after that it prints the list as expected but I don't make it to the prompt to sort still because of the error at line 44 I believe. Do I just have that line typed slightly wrong? – Stephen.J7 Nov 20 '12 at 03:26
  • fixed the printing issue but I am still getting the error at line 44. the printing issue was just a print line that I had to remove completely in get_names – Stephen.J7 Nov 20 '12 at 03:31
  • do the raw_input lines need be changed to just input? When I tried this I at least get the prompt but when I put the persons name in it only prints their first name or last name not the full name. also when asked to print again and I type Y it does nothing – Stephen.J7 Nov 20 '12 at 03:35
  • You should use "raw_input", not "input". "input" is used for python expressions, not arbitrary strings. When that is change, the program seems to work here, asking for a name and printing its index. – BoppreH Nov 20 '12 at 03:46
  • when I run it I think raw_input is being seen as a global name that is not defined. is this something that varies between versions of python? – Stephen.J7 Nov 20 '12 at 03:57
  • Yes, if "raw_input" is not available it means you are using Python 3.x. If that's the case, please update the question tags, it makes a lot of difference. – BoppreH Nov 20 '12 at 14:08