2012-11-20 118 views
0

我一直在研究这个python问题一段时间。我在一个初级班,我很困难。现在我没有收到任何错误,但是程序没有打印数据(来自names.txt)或者提示我搜索。任何帮助,将不胜感激。 -谢谢!导入文件,打印数据,搜索然后再打印 - PYTHON

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

需要调用一个函数才能运行。你可能想检查这个问题:http://stackoverflow.com/questions/419163/what-does-if-name-main-do – monkut

回答

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块应在当块嵌套

恭喜,它似乎是工作(:

+0

谢谢!现在看起来非常接近,但我仍然看到一些问题。在第44行,我收到一个错误“无法确定类型”。此外,Last,First之后的第一行打印机是水平名称列表。 。['Riggs,Jerry','Stone,Ruby','Wood,Holly',...等等... 之后,它按预期打印列表,但是我没有按照提示进行排序仍然是因为我相信第44行的错误。我只是有这样的行类型有点不对? –

+0

修复了打印问题,但我仍然收到第44行的错误。打印问题只是一个打印行,我必须在get_names中完全删除 –

+0

raw_input行是否需要更改为仅输入?当我尝试这个时,我至少得到提示,但是当我把人名放入它时,只打印他们的名字或姓氏而不是全名。当被问及再次打印时,我输入Y它什么都不做 –

0

main()不会做太多,它只是打印几行。你会想让它叫你的其他功能。

+0

哦哇......那么这是一些过程呢!我现在用我的代码更新了以前的帖子。现在我在第39行收到语法错误。 而指数

+0

线38 '而指数

+0

= NAME_LIST infile.read()'意味着'name_list'是一个字符串,而不是一个列表。如果你想让它成为一个列表,你应该使用'.readlines()'而不是'read()'。第38行的循环应该工作。 – Tim