- 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
martineau
124k29 gold badges181 silver badges319 bronze badges
2 Answers 2
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)
answered Nov 11, 2020 at 6:49
kennysliding
3,0051 gold badge17 silver badges39 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
gbruenjes
"Do not add -1 to the array"
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)
answered Nov 11, 2020 at 6:49
Harun Yilmaz
8,5893 gold badges30 silver badges41 bronze badges
Comments
lang-py
A.append(B)instead ofA = [B]?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 useappend()method.input("Input a continuous amount of integers"). It should probably say:Input an integer:or something.arrayand alist(here you have alist, not anarray)