2013-10-09 20 views
-5

這裏定義是程序:如何函數獲取調用,而不在Python

import sys 

def IndexSearchString(): 
    ''' 

    This function search a string in a password file through index and gives the result. 

    :return: none 
    :return type: none 
    :author: Neeraj  

    ''' 
    fieldindex = int(sys.argv[1])-1 
    stringsrch = sys.argv[2] 

    file_name = open("passwd", "r") 

    for store_file in file_name: 
     temp = store_file.split(":") 
     search = temp[fieldindex] 
     #print search 

     if stringsrch in search: 
      print store_file 
      #print sys.stdout.write(store_file) 
      return 

#IndexSearchString() 
+2

對不起,但你將不得不對你的問題更具體。 – SethMMorton

+0

如果我刪除最後一行IndexSearchStrind()並運行程序,它會得到執行。 –

+0

我的意思是,如果沒有定義函數,程序甚至會被執行。我是Python的新手。 –

回答

0

確保你的代碼是完全一樣的,現在是在問題(包括@Haidro編輯)

的代碼,當你在問題中粘貼它,您的建議是縮進像這樣:

def my_function(): 
    ''' 
    docstring 
    ''' 
code_intended_for_my_function() 

#my_function() 

這將導致code_intended_for_my_function執行。這是「有效的」,因爲docstring使得my_fuction的定義有效(它什麼都不做),然後立即執行code_intended_for_my_function。要測試這一點,請在您的代碼中刪除docstring並檢查是否得到IndentationError,或者只是複製當前來自問題的代碼,然後查看它是否按預期工作。

+0

我沒有使用文檔字符串 –

+0

@NeerajLad上傳文件,你正在使用的地方,所以我們可以下載並看看它。 – DanielB

+0

這裏沒有上傳的選項。其在 –

相關問題