2013-08-01 73 views
-3
def get_key_file(): 
    '''() -> file open for reading 

     Return the key file. 
    '''  

    return open(input("Enter the name of the key file: "), 'r') 

我想寫另一個函數,將返回無論在提示編寫返回另一個函數

+1

你能提供什麼這個功能會做一個例子的方法是什麼?也就是說,它會打印什麼以及它會爲用戶輸入返回什麼?清楚地標記事件序列,而不是像註釋中那樣從python shell粘貼未格式化的文本。 – millimoose

回答

0

這是將返回用戶輸入

def getInput(): 
    userInput = raw_input("Enter the name of the key file: ") 

    #you can check to see if the input is valid here 

    return userInput 

這是你如何調用該方法

keyFileName = getInput() 
+0

我需要保留我給出的功能,並且編寫另一個功能,而不是我給出的功能,這是我所描述的我想要的功能 – user2639519

+0

@ user2639519這就是我認爲的這個新功能 – Stephan

+0

>>> getInput() 輸入密鑰文件的名稱:twins.txt 'twins.txt' – user2639519

0

輸入的用戶嘗試捕捉)在它自己的變量輸入的返回值(輸入值的新功能。然後您可以打印該值並將其用於打開。如果您需要能夠在其他地方使用的文件名,你可以同時返回的開放)的結果(和輸入(的結果),像這樣:

def openFile(): 
    fileName = input("Enter the name of the key file: ") 
    fileHandle = open(fileName, 'r') 
    return fileName, fileHandle 

,然後用它是這樣的:

name, handle = openFile() 

python處理爲您返回多個值。

相關問題