There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Hello, I have a question on this exercise. My solution was accepted, but I didn’t do what the hint describes (which I checked afterwards to compare notes).
The objective is to write a factorial function that does not use recursion. Hint says:
# initialize a result variable set to 1
# loop while the input does not equal 0
# reassign result to be result * input
# decrement input by 1
# return result
What I did instead:
product = n
for number in range(1, n):
product = product * number
return product
Any reason not to do it like this, or is this not what was meant by iteration?
def factorial(n):
if n < 0:
ValueError("Inputs 0 or greater only")
total = 1
for num in range(1, n + 1):
total *= num
return total
# test cases
print(factorial(3) == 6)
print(factorial(0) == 1)
print(factorial(5) == 120)