1

I'm using python 3.7.2 shell in linux mint 32bit. when I run my def of factorial(as shown in code) it says that "" operator can't be apply for 'int' & 'nonetype' but i am using "" inside print function but that does no work. even though i am not able to use return statements inside function in script mode although it works in interactive mode. help me how can i use return statements inside function in script mode & please fix my factorial code so it works.

def factorial(n):
    if n == 0:
        results = print(1)
        return results
    else:
        x = factorial(n-1)
        result = print(n*x)
        return result

factorial(4)

the error that i get when using this is

File "/home/Location", line 12, in factorial
    result = print(n*x)
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'

when i run in interactive mode it gave this error

SyntaxError: inconsistent use of tabs and spaces in indentation

I expect the factorial evaluation 4! = 24 but it gave the error shown 2nd code in script mode, and 3rd code error in interactive mode.

Saurabh
  • 52
  • 8
  • Possible duplicate of ["inconsistent use of tabs and spaces in indentation"](https://stackoverflow.com/questions/5685406/inconsistent-use-of-tabs-and-spaces-in-indentation), which is by the way the first question that pops up after googling your error message – Nino Filiu May 23 '19 at 17:21
  • but i've used only 4 spaces you can see that in my code. pleasse let me know the wrong thing about my factorial fuction. – Saurabh May 23 '19 at 17:30
  • Your code is not a [mcve]. The code you posted have the right usage of tabs and spaces, yet you claim that you have an error. Please post either the right error message or the right code. – Nino Filiu May 23 '19 at 17:35
  • probably in interactive mode, by mistake you might have given a tab instead of 4 spaces.. code snippet in the question doesn't have any indentation issue – Anvesh Yalamarthy May 23 '19 at 18:54

2 Answers2

1

print(1) will return NoneType and hence when you do a recursive call, instead of 1, you are actually sending NoneType. Separate the assignment and print as shown below and your program works:

def factorial(n):
    if n == 0:
        results = 1
        print(results)
        return results
    else:
        x = factorial(n-1)
        result = n*x
        print (result)
        return result

factorial(4)
Anvesh Yalamarthy
  • 1,625
  • 20
  • 36
  • What did you use "print" and "return" both as they both do almost same work one works in interactive in another in script mode. Suggest me if I'm wrong. – Saurabh May 24 '19 at 13:42
  • I used both, as you were trying to do both in your code, and ideally your code doesn't work in interactive mode as well.. probably you just got another error before you get `NoneType` error – Anvesh Yalamarthy May 24 '19 at 17:19
0

The issue is that you try to multiply a number with the result of print, which is of type NoneType, thus the error. Your factorial function should return a number, instead of printing it.

This code works:

def factorial(n):
    if n==0:
        return 1
    else:
        return n*factorial(n-1)

And only then should you print your results, for example by:

print(factorial(10))
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
  • But I've multiplied 'n' which is an integer and 'x' which is a function so there should not be any problem while multiplying them. As NoneType is value after print statement not inside print statement. So how I'm multiplying 'NoneType' & 'Integer' if I'm not wrong I'm multiplying 'Int' and 'Function'. – Saurabh May 24 '19 at 13:40
  • When invoking `factorial(n)`, you'll end up invoking `factorial(1)`, which will invoke `1*factorial(0)`, which is equivalent to `1*print(1)`, which will throw the error you observe. Do you understand where you're wrong now? – Nino Filiu May 24 '19 at 15:15
  • Yes thanks, Now I understand that at the end of flow of execution it will have ```print(print(1)*n)``` and here it is ```NoneType``` and ```Int``` multiplied. Thanks you so much. – Saurabh May 24 '19 at 15:43
  • Could you please tell me that the code you suggested me ```def factorial(n): if n==0: return 1 else: return n*factorial(n-1) ``` works in Interactive mode works but what to modify so that it'll work in script? – Saurabh May 24 '19 at 15:47
  • Yes, this code should work both in script mode and in interactive mode. – Nino Filiu May 25 '19 at 11:25