Day 057 - Project 57 : Factorial Calculator

If you have any questions, comments or issues with this project please post them here!

I think the example in the challenge for day 57 is misleading… it shows that here should be an input in the solution but when I go to David’s answer it’s not needed. I also tried to use his code to allow an input but it wasn’t working. Do you know how I could have done that? Thanks in advance!

@laurenjane I just finished this lesson and saw you never got a reply to your question. Here is my code that takes user input of the number to find the factorial of. Hope this answers your question if i misunderstood apologizes

def recursion(value):
  if value <= 1:
    return value
  else:
    return(value * recursion(value - 1))

print("🌟Factorial Finder🌟")
fractal = int(input("Enter a number: > "))
print(f"The factorial of {fractal} is {recursion(fractal)}")
2 Likes

Your response made a ton of sense and helped me understand this simple concept more. I tend to overthink things and your code was straightforward!

1 Like

I want to make the function return a variable (result) that I can print in the solution.
Why is it not working? :sob:

def factorial(value):
  if value <= 1:
    return value
  else:
    result = value * factorial(value-1)
    return result


value = int(input("Input a number > "))
factorial(value)
print(f"The Factorial of {value} is {result}.")

The error I get is that result is not defined !?

Result is only in scope of the elke in the function. This means it is only declares in the else and as such not visible outside of it. But you return it so… this should tell you where your error is without me writing code :slight_smile:

3 Likes

omg! so simple haha
thank you ^.^

I couldn’t get through this one… I had to nose around in here…
I’m sad duh… :smiling_face_with_tear:

Is someone able to explain to me what this code is doing? I was trying to work out a bunch of if statements and variable = statements, but then had to peek at the solution, and found the code to be not super explanatory to me as to what is actually happening. I am not understanding at any point how it is getting the 12345 (etc) to actually work. I moved on to the next lesson - Debugging - and then went back and stepped through the program to see what was happening, and I still don’t get it. I’m thinking it’s something I’m not understanding at the Return call, but can’t figure it out.

The code that I am frustrated by:

def factorial(value):
  if value == 0:
    return 1
  else:
    return value * factorial(value-1) ##this is where I get lost*

number = int(input("Input a number to get the factorial > "))
print(factorial(number))

In the future, please wrap your code in code blocks, like so:

```python
# Code here
```

This returns value multiplied by the return value of factorial(value-1).

If you printed before it got to the return each time, you’d see something like this (Assuming the print would be print(f"This value is {value}, next one is {value-1}."), and value starts at 10):

This value is 10, next one is 9.
This value is 9, next one is 8.
This value is 8, next one is 7.
This value is 7, next one is 6.
This value is 6, next one is 5.
This value is 5, next one is 4.
This value is 4, next one is 3.
This value is 3, next one is 2.
This value is 2, next one is 1.
This value is 1, next one is 0.
1 Like

Thank you. Still new to the forum - wasn’t aware of the wrap for code.

I kind of understood what was going on there. What it is that I’m missing out on is where the numbers are getting stored, so that it can conduct the math to produce the final product. I may be just overthinking it. But at least with what you’ve shown there, I can visualize a bit better what is going on. I appreciate you taking the time to reply.

1 Like

Each call to factorial has it’s own copy of the value variable, so it’s fine for it to be nested the way it is.
You could think of it like this:

factorial(10) # Clone 0, value 10
  factorial(value-1) # Clone 1, value 9
    factorial(value-1) # Clone 2, value 8
      factorial(value-1) # Clone 3, value 7
        # This continues until value is 0
2 Likes

Ah, that makes more sense. I felt it was something like that, but just wasn’t sure when/where the numbers were being held to do the final math.

Thanks again. I appreciate it :+1:

2 Likes