mohammad shahbaz Khan
mohammad shahbaz Khan

Reputation: 113

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

I am giving my input file:

2 1  

I have written a code to find probabilities (specific to my work):

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())  

I am getting error at line where I am calculating the probabilities:

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

I don't know how to resolve it.

Upvotes: 1

Views: 2354

Answers (1)

Navith
Navith

Reputation: 1069

Your fact function returns None for 0 instead of 1 because you indented return f one extra level.

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

Really, you should just use math.factorial for this.

from math import factorial as fact

Upvotes: 5

Related Questions