Factorial Calculator (WSQ09)

This code will be about creating a factorial calculator and we’ll keep practicing with the “while” loop.

Before I show you how to do the program, let’s review what a factorial is. According to this webpage, isthe product of a given positive integer multiplied by alllesser positive integers: The quantity four factorial (4!) = 4321 =24. Symbol: n!, where n is the given integer.”

Now we’re ready to start programming.

factorial1

First, I’ll make the main code. It’ll make the calculator’s work. As always, we ask the user for the number with an input. Then we convert the variable to an integer so the program is able to use it. The counter is designed with the value “1” just like the R variable.

Is necessary to do this before the while loop because if we just start using variables without specify their initial value, Python won’t recognize them and the program won’t run.

What the loop does is simple, it just multiplies the consecutive numbers from 1 to the number that the user gave us. For that reason the “cont” variable increments one unit each time the code runs. The loop will end when the counter gets bigger than the variable that the user typed.

Up to here, when we execute the program,  the terminal window will show this:

factorial2

And NOOOOW we’ll insert another loop to ask the user if he/she would like to use the program again.

factorial3

You can notice that I added another variable with the “yes” assignation. This means that WHILE the answer to the question in line 11 is “yes” the program will keep running. If the user types something different, the loop will end, and the word “thanks” will be printed.

You can be aware that the

factorial4.png

loop “wraps up” the main code, this is because if the user wants to use the program again, the operation needs to run all over again.

factorial4.png

That’s the result.

Here’s my code, if you want to check it out.

Have a nice day!

 

 

CC BY-SA 4.0 Factorial Calculator (WSQ09) by paogarcia2401 is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.