wsq06

--Originally published at Tomas Enciso

Probably one of the most frustrating programs that I had to write just because I tried so many different things. This program is a factorial calculator, basically getting a number from a user and returning the factorial.

Screen Shot 2017-05-04 at 6.42.06 AM

Screen Shot 2017-05-04 at 6.42.38 AM

Here I entered 4 and the function returned 24, this function works by multiplying 4 times 3 times 2 times 1, the for loop is controlled by the number the user inputs.


WSQ-06

--Originally published at Program

Create a program that asks the user for a non-negative integer (let’s call that number n) and display for them the value of n! (n factorial).

After showing them the answer, ask them if they would like to try another number (with a simple y/n response) and either ask again (for y) or quit the program and wish them a nice day (if they answered n).


# WSQ06 – Factorial Calculator

--Originally published at マルコ

What to Do

Create a program that asks the user for a non-negative integer (let’s call that number n) and display for them the value of n! (n factorial).

After showing them the answer, ask them if they would like to try another number (with a simple y/n response) and either ask again (for y) or quit the program and wish them a nice day (if they answered n).

Details

For the Python group, resist the urge to call math.factorial(n). Yes that would solve the problem but what would we do if there was no math.factorial() and we had no internet to find someone’s solution?

There are two basic approaches: a loop with an accumulator of the multiplication and a recursive solution. Choose one and implement that. Once that is done, try the other way.

If you used a while loop for the solution with a loop, try structuring this with a for loop (or vice-versa).

factorial

factorial2

Featured image:

 

 


Factorial Calculator – WSQ06

--Originally published at Programming

Hello! Today we are doing a factorial calculator. This is about calculate the factorial of a number, asking the user for a non-negative integer. If you don’t know is the factorial or you want to refresh, you can know about it in Wikipedia-Factorial.

Well, as usual, here is the code: (and then I will explain)

Captura de pantalla 2017-03-20 a la(s) 08.26.41

First we print an introduction phrase for the user. The objetive of this exercise is to use loops. To start the loop our coindition needs to be true, thats why we declared repeat as “yes” and the we start the loop.

Inside this loop, we ask for the number like an int. Then we inicialize the counter like one and the fact too, fact is the number that will be multipliying and incrementing. For that happens we made another loop. The condition as we see is that the counter is different than the number (that the user give us) plus one. This way then we see that fact will be multiplicating the counter and after that it will increase one.

After this math operations we print the result with a sentence to advise, and finally we ask the user if he want to repeat the cycle.

If yes, all start again. If no, we just print Thanks.

And that’s it. This is how it runs:

Captura de pantalla 2017-03-20 a la(s) 08.27.14

Hope it helps ?


WSQ-06

--Originally published at Héctor Santillán

En esta ocasión, debemos de realizar un programa que devuelva el factorial del número ingresado, aunque sólo si este es un número Entero Positivo. De lo contrario, pedimos al usuario que ingrese un entero positivo para que el programa cumpla su objetivo.

captura-de-pantalla-17

Ejecutado en Python:

captura-de-pantalla-18

Otra vez no ocupé de algún conocimiento previo, sino simplemente aplicar lo anteriormente visto.


Factorial calculator!

--Originally published at Python learning

In this exercise I had to ask the user for a positive number and display its factorial. The program had to continue running until the user indicated it to stop by answering “n” to the question “would you like to try again?”.

For this, I used if-else and a loop, both of which I have discussed in previous exercises (You can check it out specifically here!).

You can look at the code I made for this:factorial

And how it runs:

factorial-run


WSQ06 – ‘While’ Loops (Python3)

--Originally published at Elu's Blog

For this assignment these were my instructions:

Create a program that asks the user for a non-negative integer (let’s call that number n) and display for them the value of n! (n factorial).

After showing them the answer, ask them if they would like to try another number (with a simple y/n response) and either ask again (for y) or quit the program and wish them a nice day (if they answered n).

This is what I came up with:

Captura de pantalla 2017-02-14 a la(s) 12.06.28.png

Captura de pantalla 2017-02-14 a la(s) 12.06.54.png

A ‘while’ loop is very similar to a ‘for’ loop in a way that it repeats a certain task until you tell it to stop. In this case, I made two loops. The first one takes the variable ‘counter’, and the variable ‘n’ and it subtracts 1 to it. The instructions inside that loop will repeat until the argument ‘counter < n-1’ stops being true. To stop this loop for being an infinite loop, every time the loop runs, it will add 1 to the variable counter.

The second loop has the argument ‘True’, so in theory it will run forever. The thing is that in line 14 I added the word ‘break’, what this does is that it stops the loop. So whenever the variable ‘follow’ is assigned the value ‘n’, the loop will stop.

Alternative Explanation

“Python 3 programming tutorial: While Loop” by sentdex