Search code examples
pythonfunctional-programmingfactorial

Python program for factorial coming up with an error


I was trying to write a basic factorial program to get some practice in python and i came up with this error for the following code. Any help would be appreciated.

def factorial(x):
    x = raw_input(":")
    if x == 0:
        return 1
    else: 
        return x * factorial(x-1)

when i run this program through the terminal, it gives me the following error:

**Line 1: syntax error near the unexpected token '('

line 1: def factorial(x)**


Solution

  • In addition to indentation mistakes, you need to move this line out of the function,

    x = raw_input(":"),
    

    or you have to input a number at every level of recursion.