0
  • Declare an empty array
  • Let the user add integers to this array, over and over
  • Stop when the user enters -1
  • Do not add -1 to the array
  • Print the 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
4
  • 1
    A.append(B) instead of A = [B]? Commented Nov 11, 2020 at 6:46
  • A = [B] - This won't work. Here you are creating an array of one element, but your task says that you need to "add integers to array". You need to use append() method. Commented Nov 11, 2020 at 6:47
  • Also this message makes no sense: input("Input a continuous amount of integers"). It should probably say: Input an integer: or something. Commented Nov 11, 2020 at 6:49
  • For the future please remember what @BurningAlcohol said in his answer: There is a difference between an array and a list (here you have a list, not an array) Commented Nov 11, 2020 at 7:38

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

"Do not add -1 to the array"
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.