# Factorial

#1

Hey guys - could someone give me a hint as to what I can do to change my code to be correct for this factorial exercise? See below. I’m not sure if I need the for loop under the while loop, sort of hard to conceptualize. Any help is appreciated. Thanks.

``````def factorial(x):
total = 0

while x-1 > 0:
for n in x:
total += n * (n-1)
if x-1 <= 0:
total = 1
``````

FAQ: Learn Python - Practice Makes Perfect - factorial
#2

no, a for loop with range or a while loop will do. Using both will just complicate your program unnecessary.

you can make it yourself a lot easier to when declaring (naming) total, to give it a value of 1. Because zero * anything is always zero.

#3

Refer to how you’d compute factorial manually, you’d count up from 1 to x which tells you that this takes one loop, and it tells you where it should start and where it should stop.
(And if you haven’t figured out how to do it manually yet then you can’t possibly write the code either, so start by deciding on what needs to be done before you write anything at all)

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.