Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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 3.x_String Formatting - Fatal编程技术网

Python 使用输入函数时格式化提示

Python 使用输入函数时格式化提示,python,python-3.x,string-formatting,Python,Python 3.x,String Formatting,我正在尝试编写一个脚本,它将提示用户回答一个加法问题。要做到这一点,我需要将一个变量传递给提示期间显示的字符串,但我很难弄清楚如何做到这一点 我想出了以下不起作用的代码: operand = 4 ans = input('What is 4 + {0} ?') .format(operand) 返回: 'What is 4 + {0} ?' 但我希望它是: 'What is 4 + 4 ?' 我该怎么做 ans = input('What is 4 + {0} ?'.format(oper

我正在尝试编写一个脚本,它将提示用户回答一个加法问题。要做到这一点,我需要将一个变量传递给提示期间显示的字符串,但我很难弄清楚如何做到这一点

我想出了以下不起作用的代码:

operand = 4
ans = input('What is 4 + {0} ?') .format(operand)
返回:

'What is 4 + {0} ?'
但我希望它是:

'What is 4 + 4 ?'
我该怎么做

ans = input('What is 4 + {0} ?'.format(operand)) 
ans = input('What is 4 + {0} ?'.format(operand)) 
您需要修改字符串参数以进行输入。在代码中,您试图格式化
input
函数返回的输入字符串


您需要修改字符串参数以进行输入。在您的代码中,您试图格式化由
输入
函数返回的输入字符串

格式化是字符串上的一个方法,如下所示:


format是字符串上的一个方法,如下所示: