0

So why does this code not print previous example as a list? (Please!)

def countdown(): i=5 while i > 0: yield i i -= 1 for i in countdown(): print(list(countdown(i)))

25th Nov 2016, 11:03 AM
Chris
2 Antworten
+ 3
Because countdown() iterator takes no arguments. And you are calling it with one: countdown(i). You should ether iterate countdown or list it, not both: >>> for i in countdown(): #iterate countdown print(i) 5 4 3 2 1 >>> print(list(countdown())) #list countdown [5, 4, 3, 2, 1]
25th Nov 2016, 7:16 PM
donkeyhot
donkeyhot - avatar
0
countdown(i) won't work. If you want countdown to take a parameter with a default value, you should use def countdown(i = 5): while i > 0: ...
25th Nov 2016, 11:41 AM
Samuel Neo
Samuel Neo - avatar