+ 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

9th Jun 2019, 8:29 AM
Raj kumar Darshanala
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 ...
9th Jun 2019, 9:57 AM
Théophile
Théophile - avatar
+ 2
So write : yield x * next(factorial(x - 1))
9th Jun 2019, 9:58 AM
Théophile
Théophile - avatar