Nott
Nott

Reputation: 1

How to update a list continuously?

 While true 
  a = input ( “Enter number “)
  b=[1,2,3,4]
  b.append(a)

I want to add all the values of a to the list when the user inputs a value again and again but it always replaces the old value of a with the new one .

Upvotes: 0

Views: 58

Answers (1)

Siong Thye Goh
Siong Thye Goh

Reputation: 3586

In your code, b is always reset to [1,2,3,4] inside the loop and then a new number is being appended to it, hence it gives us the illusion that it is replacing the old number.

b should be initialized outside the loop:

b=[1,2,3,4]
while True:
    a = int(input ("Enter number "))
    b.append(a)

Remark: currently this is an infinite loop. You might want to think of an exit condition.

Upvotes: 1

Related Questions