-3

So these are the instructions:

"Given a number “n,” if I were to multiply all positive integers up to and including “n,” I would get a product. This product is called the factorial of “n,” which is denoted n! (read as n factorial). It is also given that 0! = 1. Your task is to write a Python script that implements the factorial of a number “n.” For example if “n” is initialized with the value 5, then your script should print out the following.

The factorial of 5 is 120"

I'm stuck on how exactly to structure the while and for loop along with the range variable. Can any one help me here. It doesn't have to be the exact answer. Just a clue or something please :D

  • 1
    Please learn about for loops. –  Sep 11 '16 at 21:27
  • 1
    Giving me tasks... Your funny. – thesonyman101 Sep 11 '16 at 21:33
  • Oh well. That's really the simplest way to solve the problem! –  Sep 11 '16 at 21:35
  • 1
    There's literally millions of examples of doing exactly this out there. – Chris Sep 11 '16 at 21:35
  • 1
    This question has been answered countless times already. You have shown absolutely no effort to solve this question on your own (or to even Google for an answer). You literally just copied and pasted a question from your homework assignment and expect us to do it for you. And besides, using someone else's solution will only inhibit your learning in the long run. – pzp Sep 11 '16 at 21:40
  • Use the `math` module in python. –  Sep 11 '16 at 21:44
  • Actually I attempted at googling it but because whenever it comes to asking stuff I suck at wording it right, it sent me all over the place. and actually I have quite a bit of scribble in my note book on how I could have gone about it. – Alexis Nonya Sep 13 '16 at 00:20

2 Answers2

2

I hope this is not a homework question...

Here is pseudocode:

  • Create a variable to store your answer, and initialize it as 1
  • Loop through the numbers from 1 to n inclusive, multiplying your answer variable by each number
  • Print out the answer in the desired format

Thats all!

puzzled123
  • 312
  • 1
  • 2
  • 12
  • Good, that was what i was saying –  Sep 11 '16 at 21:26
  • It is a homework question DX. Sorry I should have specified. I thought the " don't have to give the exact answer" would have said that. O_O – Alexis Nonya Sep 11 '16 at 21:27
  • 2
    don't worry, it's still challenging me to actually put the thing in the code XD – Alexis Nonya Sep 11 '16 at 21:29
  • 1
    Uh... where's the print the variable statement? –  Sep 11 '16 at 21:42
  • I deleted my answer as it was probably too much for a homework question. Try to implement puzzled123's pseudo code and if you get stuck and frustrated post a new question with the code you have. Good luck! – Abd Azrad Sep 11 '16 at 21:45
0

This simple for loop works:

prod = 1  # solution variable
for i in range(1, n+1):
    prod *= i  # do the work, i.e., make the solution
print("The factorial of %s is %s" % (n, prod))

We don't need to check for zero as the loop is skipped if so.

Hope this helps!

(Alternatively, you could use math.factorial(n), but you aren't learning anything if you use builtins)

Tell me if you have more questions.