+ 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
2 odpowiedzi
+ 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)
+ 4
#Try list comprehension
[print(i*i) for i in range(1,5)]