From the course: Python Essential Training

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Solution: Factorials

Solution: Factorials

- [Instructor] I hope you had fun figuring out those factorial functions. Welcome back. How'd you do? I'll show you my solution, and if you looked at the hints file, this solution probably shouldn't come as too much of a surprise. The first thing we want to do is make sure we're able to take the factorial of our input, so if our input is not an integer, we're going to return none. So this takes care of floats like 1.2 as well as the string "spam, spam, spam, spam, spam, spam" that you might have noticed was a test case. Of course, negative two is still a valid integer, so we need to make a special case for that as well, so we check to see if the input number is less than zero and then we return none, because you cannot have the factorial of a negative integer. Now we want to get down to the business with calculating the factorial. To do this, I'm going to make two variables, fact, short for factorial. This is the thing that we ultimately return, and then counter to keep track of how…

Contents