Why my code running None. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why my code running None.

def sq(x): for i in x: a=(i*i) z=[] print(z.append(a)) a=[1,2,3,4] sq(a) output=None None None None

16th Mar 2018, 4:04 PM
Maninder $ingh
Maninder $ingh - avatar
2 Answers
+ 4
your loop contains z=[] which means each time the loop runs it is clearing the list. also, your trying to print a function that has no value. it is a function to add something to a list. if you want to print each letter being added, just print a. if you want to print the growing list, just print z try this Why my code running None. def sq(x): z=[] for i in x: a=(i*i) z.append(a) print(a) a=[1,2,3,4] sq(a)
16th Mar 2018, 4:17 PM
LordHill
LordHill - avatar
+ 4
#Try list comprehension [print(i*i) for i in range(1,5)]
16th Mar 2018, 6:17 PM
Louis
Louis - avatar