+ 2
Plz Explain the question
def f(r): j = range(1,r+1) return eval("*".join([str(i)for I in j]) print(f(4))
2 odpowiedzi
+ 2
First, let me explain what eval is.
eval means evaluate and it evaluates the string to be specific.
For example:
x = " 2 + 2 "
print(eval(x))
>> 4
- - - - - - - - -
So in your code, you called the function with argument 4.
j = range(1, 5)
j is an iterator from 1 to 4
"*".join means it will convert the list to string with * between each element.
So, our list or iterator is like this: [1, 2, 3, 4]
Then we will convert it to string with * in between.
" 1 * 2 * 3 * 4 "
- - - - - - - - - - - - - -
Then finally, evaluate the given string using "eval" method/function.
eval ( " 1 * 2 * 3 * 4 " )
1 * 2 * 3 * 4
24
return the 24 and print the returned value
Output:
>> 24
And by the way, that function is to solve the factorial of a number using iterators instead of recursion..
If my explanation is still unclear, please feel free to ask. Thanks!
+ 5
《 Nicko12 》 thank you so much brother..