Calculate a Factorial Using While Loops

So I have this practice assignment to calculate factorials, I did tried to google it since I’m sure it’s a common topic and did get a bunch of hits, but some of them are using an “f” function, that has not come up yet in class, so I’m not sure that is what this question is looking for. Below is the question and then what I’ve come up with so far. Any help is greatly appreciated, and maybe an explanation as I don’t want just an answer, no point in asking for an answer if I don’t understand the concept. Again, any guidance is greatly appreciated.

Write a program that lets a user enter N and that outputs N! (N factorial, meaning N*(N-1)(N-2)21). Hint: Initialize a variable total to N (where N is input), and use a loop variable i that counts from total-1 down to 1. Compare your output with some of these answers: 1:1, 2:2, 3:6, 4:24, 5:120, 8:40320.

This is what I have so far:

total = int(input()) # Read user-entered number
i = -1
while i <= total:
print(total *(i - 1))
i = i + 1

You are close!

I think you misread the assignment as you set i equal to -1. This should just be set to 1.

print(total *(i - 1))

Also there is no need to do i -1 here, just i will suffice.

The assignment also calls for a “for” loop, using a “while” here needlessly complicates things. Try to rewrite this using range().

Good luck!

0! = 1
1! = 1
2! = 2 × 1
3! = 3 × 2 × 1
4! = 4 × 3 × 2 × 1
and so on …
I would recommend having a variable to store the product so far, that is separate from the variable used to check where the loop should end:
(also, you want to avoid multiplying by 0 at any point)

spoiler:

endNumber = int(input()) # Read user-entered number
product = 1  # to store product so far; using 1 because any number * 1 = same number 

i = 1  # start with 1
while (i <= endNumber):  # stop when reach endNumber 
 # i would be 1, 2, 3, ... , endNumber
 product = product * i  # update product to be result of multiplying product by i
 i = i + 1

# the result is the product of 1, 2, 3, ... , endNumber 
print(product)
# fancy printing using f-strings:
print(f"{endNumber}! = {product}")

The loop is there so the product is multiplied by 1 on the first iteration
then the product is multiplied by 2 on the second iteration,
and so on …
until product is multiplied by endNumber
and the loop stops after that

We haven’t gotten to For Loops, nor Range, which is why none of the answes I’m able to find are working, I tired resetting i to 1 but just gave me an endless loop, I think I might need to walk away from the screen for a second and see if it refreshes my mind… lol I’ve emailed the professor to see what he suggests. I’ll hope to figure it out here soon… thanks for the help!

Indeed, I see that I made the for loop requirement up myself :stuck_out_tongue:

If by resetting i to 1 you mean that you are resetting it in the loop, then yes, this will indeed result in an endless loop.

i = -1

should be

i = 1

The answer of @janbazant1107978602 correctly mentions that you should use a separate variable to store the results, this will allow you to keep track of the total result instead of printing them separately.

1 Like