我正在提供我的输入文件:

2 1


我已经编写了代码来查找概率(特定于我的工作):

def fact(x):
    f=1
    if x > 0:
        for i in range(1,x + 1):
            f = f*i
        return f


def allele(s):
    n,k=[int(i) for i in s.split()]
    summ=0
    for i in range(n,((2**k)+1)):
            if i < (2**k +1):
                probability = (fact(2**k)/(fact(i)*fact((2**k)-i)))*(0.25**i)*(0.75**((2**k)-i))
                summ=summ+probability
    print summ

allele(open('D:\python\input.txt', 'r').read())


我在计算概率的那一行出现错误:

unsupported operand type(s) for *: 'int' and 'NoneType'


我不知道该怎么解决。

最佳答案

您的fact函数返回None而不是1为0,因为您缩进了
return f额外一级。

def fact(x):
    f = 1
    if x > 0:
        for i in range(1, x + 1):
            f *= i
    return f


确实,您应该为此使用math.factorial

from math import factorial as fact

关于python - python中的错误:*:'int'和'NoneType'不受支持的操作数类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29724773/

10-13 09:22