0

我一直在研究这个 python 问题。我正在上入门级课程,我被卡住了。现在我没有收到任何错误,但程序没有打印数据(来自 names.txt)或提示我搜索。任何帮助,将不胜感激。-谢谢!

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()
4

2 回答 2

0

main()没有做太多,它只是打印几行。你会想让它调用你的其他函数。

于 2012-11-20T02:37:03.943 回答
0

修改最少的更正版本:

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()

发生了什么变化:

  • 在 get_names 中,读取的 name_list 实际上是一个字符串,而不是一个列表。要获取需要在换行符 ( .split('\n')) 上拆分的行列表。之后,您不需要从名称中删除换行符

  • get_names() 返回后,需要存储列表并传递给其他函数

  • new_file 使用了 name_list,但没有将其作为参数接收

  • try/except 块应该嵌套在 while 块中

恭喜,它似乎正在工作(:

于 2012-11-20T03:11:49.453 回答