-3

Please tell me why this python code to find factorial is incorrect. I used while loop, and it's not working, I don't know why.

n = input("Enter number ")

def factorial(n):
    while n >= 0:
         if n == 0:
             return 1
         else:
             return n * factorial(n-1)
    print("Incorrect Input")

Stef
  • 13,242
  • 2
  • 17
  • 28
  • 2
    You need a while loop *or* a recursive call, not both. – khelwood Mar 24 '22 at 16:01
  • 2
    First of all, `input()` returns a string, not an integer. You need to convert it if you want to use it as a number. – matszwecja Mar 24 '22 at 16:03
  • @khelwood The `while` loop is not infinite. In fact, it behaves exactly as if it was `if n >= 0:` rather than `while n >= 0:`; because the `return` statement interrupts the loop. – Stef Mar 24 '22 at 16:03
  • @Stef You are quite correct. I have excised that part of my comment. – khelwood Mar 24 '22 at 16:03
  • 3
    What's the point of using a loop if you return in both branches of the `if`? The return statement ends the loop since it exits the entire function. – Barmar Mar 24 '22 at 16:21
  • @matszwecja The question is tagged `python-2.7`. `input()` in Python 2.x parses the value automatically. – Barmar Mar 24 '22 at 16:22
  • Dear AyushSeth, can you please clarify the following points: (1) Are you using python2 or python3? (2) What do you mean "it's not working"? What happens when you try to run it? Is there an error message? If yes, please copy and paste the error message. – Stef Mar 24 '22 at 16:24
  • What incorrect result are you getting? – Barmar Mar 24 '22 at 16:25
  • @matszwecja thank u for pointing the error, yes input() returns string not int.. that's why the code was not working. Now I'm clear. Thanks – AyushSeth Mar 25 '22 at 14:48
  • @stef Thanks for commenting, I coded the code in Online Python Compiler.. there because of input(), it wasn't working. But now its working as it should be. – AyushSeth Mar 25 '22 at 14:51

1 Answers1

0

There are actually several methods, but two common ones are:

  1. iterative approach (with for or while loops)
  2. recursive approach (function calls itself).

They are generally not combined. However, the error (as pointed out in the first comment here by @matszwecja) is that the top line returns a string and not an int.

iterative approach:

def factorial(n):
    fact = 1
    for num in range(2, n + 1):
        fact *= num
    return fact

recursive approach:

def factorial(n):
    if n < 2:
        return 1
    else:
        return n * factorial(n-1)

To save repetition many valid methods are discussed here: Function for factorial in Python

D.L
  • 4,339
  • 5
  • 22
  • 45
  • All the code in question needs to work is conversion of `input()` to `int`. Combining iterations and recursion has nothing to do with the error (Not saying it is a good code though - but it does what it is supposed to do) – matszwecja Mar 24 '22 at 16:17
  • @matszwecja good point. i didnt even consider the first line, but that would surely throw an error ! – D.L Mar 24 '22 at 16:19
  • `input()` in Python 2.7 parses the value, you don't have to call `int()`. – Barmar Mar 24 '22 at 16:23
  • 1
    @matszwecja. i updated the answer and referenced you. well spotted. – D.L Mar 24 '22 at 16:24
  • @D.L yes that's correct, matszwecja is right.Pointed the exact error. Thanks – AyushSeth Mar 25 '22 at 14:57