2012-11-20 102 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