Andrew Jons
Andrew Jons

Reputation: 125

Problem with Python code for finding factorials

Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it. For example: if num = 4, then your program should return (4 * 3 * 2 * 1) = 24. For the test cases, the range will be between 1 and 18 and the input will always be an integer.

and this is my code

def FirstFactorial(num):
    x = [1]
    if num == 1:
        return 1
    else:
        for i in range(1,num+1):
            x = x*(i)
    return x

print (FirstFactorial(4))

The expected output is 24. I get the following output from the code given above.

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Upvotes: 2

Views: 7020

Answers (5)

codehunter-py
codehunter-py

Reputation: 69

Actually correct one for me I got this and achieved to given test result.

def FirstFactorial(num):
  if num ==1:
    return 1
  else:
    for i in range(1, num-1/2):
       num = num*(i)
  return num

# keep this function call here 

Upvotes: 0

Xchg0x5f375d
Xchg0x5f375d

Reputation: 449

You had error in your x declaration. Also, using lambda expression.

fact = lambda n:1 if n==0 else n*fact(n-1)

print(fact(4))

>>> 24

42 bytes

fact = lambda x:0**x or x*fact(x-1)

print(fact(4))

27 bytes

Python has C type internal implementation of factorial method in math module. Since it's C, it's faster than Python's approach.

import math

print(math.factorial(4))

First answer still has for loop,

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

Upvotes: 0

Siddharth Satpathy
Siddharth Satpathy

Reputation: 3043

You have done a small mistake. Instead of defining the variable x as a list (in the first line after the definition of the function FirstFactorial(num) , x=[1]) , you want do define x as a single number, i.e. x=1.

In Python, multiplication of a list by a positive scalar (say n) leads to repetition of the numbers in the list n number of times. In other words, the * operator repeats a list for the given number of times. This is what was happening in your code in the line x = x*(i).

You can read more about python lists here.

The following code will work fine, and do your job. The only change that I have done is in the first line after the definition of the function FirstFactorial(num).

def FirstFactorial(num):
    x = 1
    if num == 1:
        return 1
    else:
        for i in range(1,num+1):
            x = x*(i)   
    return x

print (FirstFactorial(4)) 
>>24

Also, you do not need to have a separate if statement to define 1!. The following code is more concise and it will give you factorials of all numbers including 1.

def FirstFactorial(num):
    x = 1
    for i in range(1,num+1):
        x = x*(i)   
    return x

print (FirstFactorial(1))
>> 1

Upvotes: 0

Sebastien D
Sebastien D

Reputation: 4482

You could simply do:

import math
print(math.factorial(4))  

output:

24

Upvotes: 1

Vitor Falcão
Vitor Falcão

Reputation: 1049

Let's make a better code:

def factorial(num):
    f = 1
    for i in range(1, num+1):
        f = f * i
    return f

Some parts of your code have no sense at all, for example, x = [1] declares x equals a list with one element one. Then if you make list * number in python you multiply the list:

x = [1, 2]
x = x * 2
print(x) # prints [1, 2, 1, 2]

The if statement that checks if the number is 1 is not necessary using the code above.

Upvotes: 6

Related Questions