Python奇怪的减法结果

时间:2014-06-30 17:29:09

标签: python

这是我的字典:

weights = {
71.03711 : "A",
156.10111 : "R",
114.04293 : "N",
115.02694 : "D",
103.00919 : "C",
129.04259 : "E",
128.05858 : "Q",
57.02146 : "G",
137.05891 : "H",
113.08406 : "I",
128.09496 : "K",
131.04049 : "M",
147.06841 : "F",
97.05276 : "P",
87.03203 : "S",
101.04768 : "T",
186.07931 : "W",
163.06333 : "Y",
99.06841 : "V",
}

现在我做一个计算:

a = (129.08346 - 15.99940) # a = 113.08406 = "I" (in dictionary)

然后:

sequence += weights[a]

ERROR:

Traceback (most recent call last):
  File "task2.py", line 43, in <module>
    sequence += weights[a]
KeyError: 113.08406000000001

他为什么要附上那个? :(我需要值113.08406!

1 个答案:

答案 0 :(得分:1)

将结果传递给round()函数:

a = round(129.08346 - 15.99940, 5) # second argument is number of decimals

输出:

113.08406