Reputation: 17
n = -12
while n < 15:
if n < 0:
dgt = []
dgt.append(n)
n = n+1
print(dgt)
I am trying to append all negative values in dgt[] but what I get from this code [-1]
which is not my result I want all negative valuse in dgt[] please help me.
Upvotes: 0
Views: 2040
Reputation: 24603
also you know you can do it without loop :
n = -12
dgt = [n+i for i in range(-n)]
print(dgt)
Upvotes: 1
Reputation: 1095
You are reinitialising the list dgt
for every loop iteration. Move it outside the loop.
n = -12
dgt = []
while n < 15:
if n < 0:
dgt.append(n)
n = n+1
print(dgt)
Upvotes: 0