5. factorial


#1




I am trying to use result = ' * '.join(str(result_list)), but it isn't working! My code is listed below:


def factorial(x):
    result_list = list(range(x, 0, -1))
    result = ''
    for number in range(len(result_list)):
        result = ' * '.join(str(result_list))
    print result
factorial(2)


Thanks!


#2

simple question: why? What do you think it does?

I am trying to grasp the logic of your code, but i am not sure i do. I find it very clever you make a list:

result_list = list(range(x, 0, -1))

that is really good. but "".join() is for joining a list into a string, is that really what you want?

I would declare a variable total, set it to one, and then multiply it with all values in your list


#3

I want to convert it into a string because then I can eval the string


#4

But thats also a good idea... Ill try it


#5

as for your string solution, you could make an empty string. then loop over result_list and add each value to the string. don't forget to cast the integer to a string


#6

Yeah. Thanks. Ill try that too


#7

Thanks a lot for the fast reply


#8

Did you already figure out? That was fast


#9

You can use the join method for this exercise, but it is a bit awkward.

If you create a list of numbers represented as strings, you can call the join method from "*" to create a unified string, and then pass that to the eval function. The embedded "*" characters will function as * operators. However, if you plan on using range to construct your list, you must account for the fact that the factorial of 0 and the factorial of 1 are each 1.


#10

Thanks @appylpye appylpye for helping


#11

Really? I thought I was pretty slow in learning what the problem was
EDIT: Thanks for what you said below


#12

some people can't figure it out when you give a hint like that. You did really good :slight_smile:


#13