Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.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 - Fatal编程技术网

在Python中,如何将原始输入的答案插入字符串的中间?

在Python中,如何将原始输入的答案插入字符串的中间?,python,python-2.7,Python,Python 2.7,您忘记了连接的加号: def number_of_legs(): print " The sparrow has how many legs" legs = raw_input() print "You said the sparrow has ",+ legs"legs" number_of_legs() 请尝试使用字符串格式: print "You said the sparrow has " + legs + " legs" 试试这个。只需按顺序获取引号

您忘记了连接的加号:

def number_of_legs():
    print " The sparrow has how many legs"
    legs = raw_input()
    print "You said the sparrow has ",+ legs"legs" 


number_of_legs()
请尝试使用字符串格式:

print "You said the sparrow has " + legs + " legs" 
试试这个。只需按顺序获取引号、加号和逗号。

使用

将输出:(如果支腿为15)

另外,我建议您可以将字符串作为参数放置到。像这样:

You said the sparrow has 15 legs
当我运行此命令时:

legs = raw_input("The sparrow has how many legs? ")
print "You said the sparrow has {} legs".format(legs)
使用str.format

The sparrow has how many legs? 25        # 25 is my input and that's it
You said the sparrow has 25 legs

运行此操作时,您会得到:
print“you said the sparrow has”、+legs“legs”^SyntaxError:无效语法
在解决方案存在的大多数情况下,请花时间阅读控制台输出。在您的例子中,第4行显然有语法错误
You said the sparrow has 15 legs
legs = raw_input("The sparrow has how many legs? ")
print "You said the sparrow has {} legs".format(legs)
The sparrow has how many legs? 25        # 25 is my input and that's it
You said the sparrow has 25 legs
print "You said the sparrow has {0} legs".format(legs)