please help me understand where i went wrong:
this is the question:Create both a recursive function called recursive_factorial and iterative function called iterative_factorial that does the following
Accepts as parameter an Integer n
Computes the factorial of n
Returns the factorial of n
this is the test i am using for the question:
import unittest
class RecursiveTestCase(unittest.TestCase):
def test_recursive_factorial_one(self):
result = recursive_factorial(4)
self.assertEqual(result, 24, msg="Inaccurate value")
def test_recursive_factorial_two(self):
result = recursive_factorial(0)
self.assertEqual(result, 1, msg="Inaccurate value")
def test_iterative_factorial_one(self):
result = iterative_factorial(5)
self.assertEqual(result, 120, msg="Inaccurate value")
def test_iterative_factorial_two(self):
result = iterative_factorial(0)
self.assertEqual(result, 1, msg="Inaccurate value")
this is the code i have written:
def recursive_factorial(n):
if n == 0:
return 1
else:
return n * recursive_factorial(n-1)
def iterative_factorial(n):
x = 1
li = list(range(1, n + 1))
for each in li:
x = x * each
this is the error i am getting:
1 . test_iterative_factorial_one
Failure in line 21, in test_iterative_factorial_one self.assertEqual(result, 120, msg="Inaccurate value") AssertionError: Inaccurate value
2 . test_iterative_factorial_two
Failure in line 25, in test_iterative_factorial_two self.assertEqual(result, 1, msg="Inaccurate value") AssertionError: Inaccurate value
please help me understand where i went wrong.
iterative_factorial(n):has noreturnrecursive_factorial(0). The code looks correct.