为什么Python不会给我一个明确的算术==

时间:2015-05-31 19:17:48

标签: python arithmetic-expressions

我制作了这个脚本来寻找拼图的解决方案。我不得不把数字1到9通过一些算术得到66.但由于某种原因,我得到一个近似的列表,包括68.5333333333,68.6666666667,69.6等等。

这是我的代码(我非常感谢同行评审,如果可以的话):

def equation():
    # intertools gives me the posibility to permutate a list of numbers, floating points for this case so I have decimals
    import itertools
    permutationlist = list(itertools.permutations([1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0]))
    # I make a loop for every permutation, the last number is 9! for the last one
    for n in xrange (0,362880):
        currentlist = permutationlist[n]
        # every variable is taken for each item in the current list
        a = currentlist[0]
        b = currentlist[1]
        c = currentlist[2]
        d = currentlist[3]
        e = currentlist[4]
        f = currentlist[5]
        g = currentlist[6]
        h = currentlist[7]
        i = currentlist[8]
        # if statement to pick up what computes to 66
        # here is the problem, what part of == is not clear to Python?
        if ((((((((((((a+13)*b)//c)+d)+12)*e)-f)-11)+g)*h)//i)-10) == 66:
            print ('[%s]+13*[%s]/[%s]+[%s]+12*[%s]-[%s]-11+[%s]*[%s]/[%s]-10 =') % (a,b,c,d,e,f,g,h,i), ((((((((((((a+13)*b)/c)+d)+12)*e)-f)-11)+g)*h)/i)-10)
        else:
            pass
        # next list...
        n = n + 1

equation()

0 个答案:

没有答案