+ 3
in what way it was wrong
def factorial(x): if x == 1: yield 1 else: yield x * factorial(x-1) print(list(factorial(5))) i just replaced return woth yield why iam not gettling list of factotias upto 5
2 Antworten
+ 3
Factorial isn't a function but a generator. x is an integer and factorial(x - 1) déclares à generator, that return nothing...
Ex of generator :
def count():
yield 1
gen = count() #returns nothing : non initialized
print(next(gen)) #outputs 1
...
+ 2
So write :
yield x * next(factorial(x - 1))