![](/img/trans.png)
[英]Python Error: unsupported operand type(s) for +: 'int' and 'NoneType'
[英]Error in python: unsupported operand type(s) for *: 'int' and 'NoneType'
我正在提供我的輸入文件:
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
函數返回0而不是1
None
,因為您縮進了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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.