Reputation: 928
Can somebody explain why list is supposed to allow mix types but the following code does not work please (the only difference is the type declaration around the i
?
The error:
print(','.join([buzzfizz(n) for n in range(1,51)]))
TypeError: sequence item 0: expected str instance, int found
DO NOT WORK:
def buzzfizz(i):
if i % 2 == 0:
return 'buzz'
if i % 3 == 0:
return 'fizz'
if (i % 3 == 0 & i % 2 == 0):
return 'buzzfizz'
else:
return i
print(','.join([buzzfizz(n) for n in range(1,51)]))
WORK:
def buzzfizz(i):
if i % 2 == 0:
return 'buzz'
if i % 3 == 0:
return 'fizz'
if (i % 3 == 0 & i % 2 == 0):
return 'buzzfizz'
else:
return str(i)
print(','.join([buzzfizz(n) for n in range(1,51)]))
Upvotes: 1
Views: 35
Reputation: 2585
That's because the ','.join()
need a list whose elements is str
type. In your first code, the last return i
is not a str
type.
Upvotes: 3