<p>我正试图用浮点格式化程序将一行打印到2个小数点,如下所示:</p>
<pre><code> print "You have a discount of 20% and the final cost of the item is'%.2f' dollars." % price
</code></pre>
<p>但当我这样做的时候,我得到了这个错误:</p>
<p>值错误:索引27处不支持的格式字符“a”(0x61)</p>
<p>这是什么意思?我怎样才能防止它发生?</p>
<p>我认为问题在于<code>%</code>之后的单个<code>20</code>符号,python可能认为它是一个格式说明符。</p>
<p>试试这个-</p>
<pre><code>print "You have a discount of 20%% and the final cost of the item is'%.2f' dollars." % price
</code></pre>