Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将变量插入原始输入查询?_Python_Python 2.7_Variables_Raw Input - Fatal编程技术网

Python 如何将变量插入原始输入查询?

Python 如何将变量插入原始输入查询?,python,python-2.7,variables,raw-input,Python,Python 2.7,Variables,Raw Input,我已经开始学习Python2.7.x,阅读了《艰苦学习Python》一书。我目前正在学习raw_input函数,并尝试使用不同的方法。我编写了以下代码: name = raw_input("What is your name? ") print "Hi %s," % name, home = raw_input("where do you live? ") print "I hear that %s is a great place to raise a family, %s." % (hom

我已经开始学习Python2.7.x,阅读了《艰苦学习Python》一书。我目前正在学习
raw_input
函数,并尝试使用不同的方法。我编写了以下代码:

name = raw_input("What is your name? ")
print "Hi %s," % name,
home = raw_input("where do you live? ")

print "I hear that %s is a great place to raise a family, %s." % (home, name)

age = raw_input("How old are you, %s? ") % name
我在最后一行收到此错误:

TypeError:在字符串格式化过程中并非所有参数都已转换

我如何以类似的方式使用
raw\u input
函数并插入一个变量,以便自定义嵌入
raw\u input
查询中的问题(如果我把术语弄得一团糟,我深表歉意)

理想情况下,我想输出一个大致如下的问题:

鲍勃,你多大了?
试试:

age = raw_input("How old are you, %s? " % name)
说明:

raw_input([prompt])

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
所以,当你这么做的时候

age = raw_input("How old are you, %s? ") % name
假设您输入了
Paul

因此,上述声明变成

age = "Paul" % name
由于字符串“Paul”不是占位符,它会抛出相应的错误。

尝试:

age = raw_input("How old are you, %s? " % name)
说明:

raw_input([prompt])

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
所以,当你这么做的时候

age = raw_input("How old are you, %s? ") % name
假设您输入了
Paul

因此,上述声明变成

age = "Paul" % name

由于字符串“Paul”不是占位符,它会抛出相应的错误。

这似乎不起作用。查询变成:“你多大了,%s?”当我回答时,我看到了相同的错误。你是否尝试过
age=raw\u输入(“你多大了,%s?”%name)
?@PaulJacobson如果这个答案对你有效,那么你可以将它标记为已接受,这样如果将来有人在这里找到自己的方式,他们知道它似乎不起作用。查询变成:“你多大了,%s?”当我回答时,我看到了相同的错误。你是否尝试过
age=raw\u输入(“你多大了,%s?”%name)
?@PaulJacobson如果这个答案对你有效,那么你可以将它标记为已接受,这样如果将来有人在这里找到他们知道它有效的方法