Craker Box
Craker Box

Reputation: 17

How to add user input integers to array?

So far my code below:

A=[]
while True:
    B = int(input("Input a continuous amount of integers"))
    A = [B]
    print(A)
    if B == -1:
        break
    else:
        continue

Upvotes: 0

Views: 1353

Answers (2)

kennysliding
kennysliding

Reputation: 2977

In Python, we call the this [] datatype as list. To append item into a list, you can do A.append(B)

A = []
while True:
    B = int(input("Input a continuous amount of integers"))
    if B == -1:
        break
    else:
        A.append(B) # modify this line
        print(A)

Upvotes: 1

Harun Yilmaz
Harun Yilmaz

Reputation: 8558

You need to check if user input is -1 before appending it to the array and print it in the if block, append it in the else block.

A=[]
while True:
    B = int(input("Input a continuous amount of integers"))
    
    if B == -1:
        print(A)
        break
    else:
        A.append(B)

Upvotes: 0

Related Questions