0
Why this return makes invalid syntax?
def countdown(): i=5 while i > 8: print("i = {}".format(i)) yield i i -= 1 print(" =>i = {}".format(i) return i for j in countdown(): print(j)
2 Respostas
+ 4
1) missing ) in line
print(" =>i = {}".format(i)
2) you can't use return with a argument if you use that function as a generator so replace->
return i
with->
yield I
return
so the code will be->
def countdown():
i=5
while i > 8:
print("i = {}".format(i))
yield i
i -= 1
print(" =>i = {}".format(i))
yield i
return
for j in countdown():
print(j)
0
thanks