0

Hii ,I was making a factorial program in python using recursion/ but I have one doubt : if I run the program I get the error as : " result=xfact(x-1) TypeError: unsupported operand type(s) for : 'int' and 'NoneType'"
But If I run it with the commented statements it works fine .So my question is in the else why my return statent doesn't work ?

def fact(x):
    if x==1 or x==0:
        y=1
    else:
        #result=x*fact(x-1)
        #return result
        y=x*fact(x-1)
    return y

t=int(input())
for i in range(t):
    n=int(input())
    y=fact(n)  
    print(y)
3
Contributors
2
Replies
17
Views
10 Months
Discussion Span
Last Post by tinstaafl
1

When I try both sets of code with Python 3.4 in VS 2015 they seem to work without errors. Of course that's assuming that when you tried using the result variable you also changed all the instances of the y variable to result.

Edited by tinstaafl

Votes + Comments
There was some change from Python 2 that may be why they asked. But I omitted that. OP needs to tell all.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.