概述
I've tracked down a bug to incorrect placement of : in a format.
But I don't yet understand the behavior. Why does the 2nd line generate two pi's (wrong 2nd value) but proper formatting, rather than throw an exception?
import math
data = math.pi, math.e
print 'pi={:0.2f}, e={:0.4f}'.format(*data)
print 'pi={0:.2f}, e={0:.4f} wrong!'.format(*data) # wrong!
gives
pi=3.14, e=2.7183
pi=3.14, e=3.1416 wrong!
解决方案
The number before the colon is the index of the arguments, so in your second print both {0:.2f} and {0:.4f} are printing the same argument of index 0, which is math.pi.
replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"
field_name ::= arg_name ("." attribute_name | "[" element_index "]")*
Note the field_name before :, which can be an element index.
最后
以上就是魁梧篮球为你收集整理的为什么python不出结果_为什么这种错误的python格式会产生此结果,而不是异常?...的全部内容,希望文章能够帮你解决为什么python不出结果_为什么这种错误的python格式会产生此结果,而不是异常?...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复