Tsarly
Tsarly

Reputation: 11

Appending user input to a list not working

I am trying to take user input and append all numbers to a list. However the output I am getting just a blank list with nothing inside.

while True:
    my_add = input("Give me numbers and I will add them to the list. Enter 'q' to quit.")
    my_list = []
    
    if my_add == 'q':
        break
    
    else:
        my_list.append(my_add)
        
print(my_list)

Upvotes: 1

Views: 246

Answers (2)

Dan Wilstrop
Dan Wilstrop

Reputation: 445

Remove the variable my_list from the while loop as it is resetting on every iteration.


my_list = []

while True:

    my_add = input("Give me numbers and I will add them to the list. Enter 'q' to quit.")
 
    
    if my_add == 'q':
        break
    
    else:
        my_list.append(my_add)
        
print(my_list)

Upvotes: 1

furydrive
furydrive

Reputation: 510

my_list = []
while True:
    my_add = input("Give me numbers and I will add them to the list. Enter 'q' to quit.")
    if my_add == 'q':
        break
    else:
        my_list.append(my_add)
print(my_list)

my_list was set to empty list [] at every loop iteration

Upvotes: 1

Related Questions