python输入参数被添加

时间:2016-04-19 14:20:08

标签: python

如何在python的输入函数中添加prompt参数?

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

2 个答案:

答案 0 :(得分: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")

答案 1 :(得分:0)

你将python fomat与C scanf函数的使用混淆,并且错过了连接用法。您可以连接两个字符串以获取输入提示。只要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