2016-04-19 104 views
-1

如何在python的輸入函數中添加提示參數?獲取python輸入參數

def getUserFileNameInput(self, s): 
    FileName = input("Please enter the file name and path for " % s) 

test = t() 
test.getUserFileNameInput("my.txt") 

我現在的錯誤:

File "C:\Python34\try.py", line 13, in getUserFileNameInput 
FileName = input("Please enter the file name and path for " % s) 
TypeError: not all arguments converted during string formatting 

回答

1

你沒有正確連接字符串,你有,至少兩種方法來做到這一點:

myNewString = "Here's my old string : %s" % oldString 

myNewString = "Here's my old string : " + oldString 

所以在你的榜樣,你應該做的:

def getUserFileNameInput(self, s): 
    FileName = input("Please enter the file name and path for " + s) 

test = t() 
test.getUserFileNameInput("my.txt") 

def getUserFileNameInput(self, s): 
    FileName = input("Please enter the file name and path for %s" % s) 

test = t() 
test.getUserFileNameInput("my.txt") 
0

你感到困惑與C scanf函數用法蟒蛇fomat和失蹤的串連使用。您可以連接兩個字符串以獲取輸入提示。我認爲只要s已經是一個字符串,就不需要這種格式(就像你打電話時一樣)。如果你想傳遞一個值(像一個整數),你總是可以使用string(s)。不過,我簡單的連接會更好。

def getUserFileNameInput(self, s): 
    myprompt = "Please enter the file name and path for " + s 
    Filename = input(myprompt) 
    return Filename 

myentry = test.getUserFileNameInput("my.txt") 
print myentry