FAQ: Recursion vs. Iteration - Coding Throwdown - Rules of the Throwdown

This community-built FAQ covers the “Rules of the Throwdown” exercise from the lesson “Recursion vs. Iteration - Coding Throwdown”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Recursion: Python

FAQs on the exercise Rules of the Throwdown

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 (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 (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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?

Your solution is good.
Thye just suggest using while instead of for.
The result is the same.

I did:

def factorial(n):
  product = 1
  for number in range(n, 1, -1):
    product *= number
  return product

It can be done like that:

def factorial(n):
  product = 1
  while n: #n is False when equals to 0.
    product *= n
    n -= 1
  return product
1 Like

This solution will not account for factorial(0)

def factorial(n):
    product = 1
    for number in range(1, n+1):
        product = product * number
    return product

now it works =)

This is my solution:

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)