What am I missing?


#1



Am I at least on the right track here? I decided to use a conditional and subtract 1 until it completes the addition to the list. I'm not seeking the answer, just a push in the right direction and what is wrong with the code.

Thanks!


def factorial(x):
    if x == 1:
        return 1
    else: 
        numbers = []
        product = 1
        y = x
        if y > 0:
            for i in range(x):
                numbers.append(x - 1), y - 1
        for x in numbers:
            product *= x
            return product


#2

The factor of anything that is 1 or less than 1 is 1. Otherwise, your new total will start at 1 (since 0 * anything is 0) will be everything in the range from 1 to x + 1 multiplied to the total. I know you don't want the answer, but let me know if you want to see how I did it.


#3

Ok thanks, I added a conditional to return 1 for the factorial of 1 (feels kinda like cheating doing it that way) but anyways, now the code returns 12 instead of 24. I have edited my code above to reflect the update.


#4

Well you can change the x == 1 to x <= 1. You don't really need to use a list with a second for loop. In my opinion, that just over complicates stuff. It gets rid of the y = x the numbers list, and one of the for loops. I would get rid of your first loop and just take your second for loop and use the range(1, x+1) instead. Then multiply each iteration of of i in range (i wouldn't use x since you used it as parameter for the function) by the product like you are already doing. I just wouldn't use x. You are definitely on the right track.


#5

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