Question

In Python Use the Design Recipe to write a function factorial that when given a positive...

In Python

Use the Design Recipe to write a function factorial that when given a positive integer calculates the factorial. If given a negative integer or not an integer), the function should return None. If given 0, it should return 1.

Include a docstring!

Note: You may assume all arguments passed to this function will be numeric.

Write three assertEqual statements to test your function. To test and receive credit for your assertEqual statements, copy them into Part E and click 'Evaluate'.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def factorial(n):
    """
    if n is negative, then return None
    if n is zero, then return 1
    if n is positive, then return factorial of n
    :param n:
    :return: factorial of number n
    """
    if n < 0:
        return None
    elif n == 0:
        return 1
    else:
        return n * factorial(n-1)


assert factorial(5) == 120
assert factorial(0) == 1
assert factorial(-2) is None
Add a comment
Know the answer?
Add Answer to:
In Python Use the Design Recipe to write a function factorial that when given a positive...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT