Python 3.2

Factorial Calculator

  • Fully automatically graded on code quality, style, structure and functionality
  • Students write their solution in an online IDE in their LMS
  • Check for Python code plagiarism


  • Arithmetics.
  • Input function.
  • Functions.

Learning goals:

  • Learn how to use a for-loop.
Get access to an autograder, plagiarism checker and online IDE for this assignment now!


Something you will often hear about programmers is that “good programmers are lazy programmers”. Programmers like to automate their tasks and do not like repetitive tasks. In this assignment, you will learn about a new programming concept that is essential when automating repetitive tasks: loops!

Python offers two types of loops: for-loops and while-loops. For-loops iterate over a sequence (i.e. looping over all words in a sentence, or all items of a list) and while-loops repeat a task as long as a certain condition is met (i.e. repeat until we have counted until 10).

To understand the power of loops, simply look at the factorial function in mathematics. A factorial of a non-negative integer `n` (denoted by `n!`) is the product of all positive integers less than or equal to `n`. For example, the factorial of 6 (or `6!`) is `6 * 5 * 4 * 3 * 2 * 1`. If we want our factorial calculator to work with any non-negative integer `n`, we cannot hardcode this but need repetition: we need to iterate through all numbers smaller than or equal to n and multiply them. This can be done perfectly using the for-loop and `range()` function.

Even though factorials seem like a dry mathematical function, they have many real-world uses. One of the most common ones is permutations. For example in the olympic games, you have the US, The Netherlands and Japan competing in basketball. How many possible final standings are there? That would be `3!`, or `3 * 2 * 1 = 6`:

  • US, Netherlands, Japan;
  • US, Japan, Netherlands;
  • Netherlands, US, Japan;
  • Netherlands, Japan, US;
  • Japan, US, Netherlands;
  • Japan, Netherlands, US.

In your script ``, create a function `factorial(n)` that takes a non-negative integer n and returns the factorial of that number n.aIt should call this function and ask the user for input in the `if __name__ == "__main__":` block. Also add input validation and fail gracefully if the user did not input a non-negative integer by printing `“Please input a non-negative integer.”` to the screen.

Example usage:

-!- CODE language-bash -!-$ python3
Non-negative integer: 0

$ python3
Non-negative integer: 3

$ python3
Non-negative integer: 10

$ python3
Non-negative integer: -5
Please input a non-negative integer.

Learn more:

Start teaching Python now

CodeGrade is your virtual teaching assistant for your coding classroom. You teach, CodeGrade takes care of the rest.

With our 40+ built-in fully automatically graded Introduction to Python assignments for university-level courses, you can start teaching Python at your institution with confidence now. Or, scale your classroom with CodeGrade's tools.

Book a personal 30-minute demo now and we'll show you how to get started with CodeGrade in less than 1 hour!

Book a demo now!

Happy educators

  • Educators who use CodeGrade have less stress.
  • Educators who use CodeGrade have happier and more successful students.
  • Educators who use CodeGrade have a simpler teaching workflow.

Book a quick 30-minute demo and see how it works for you!