Python 如何向数组中添加用户输入整数? 声明一个空数组 让用户反复向该数组添加整数 当用户输入-1时停止 不要将-1添加到数组中 打印阵列
到目前为止,我的代码如下:Python 如何向数组中添加用户输入整数? 声明一个空数组 让用户反复向该数组添加整数 当用户输入-1时停止 不要将-1添加到数组中 打印阵列,python,Python,到目前为止,我的代码如下: A=[] while True: B = int(input("Input a continuous amount of integers")) A = [B] print(A) if B == -1: break else: continue 在Python中,我们将this[]数据类型称为列表。要将项目附加到列表中,您可以执行a.append(B) 在将用户输入附加到数组
A=[]
while True:
B = int(input("Input a continuous amount of integers"))
A = [B]
print(A)
if B == -1:
break
else:
continue
在Python中,我们将this
[]
数据类型称为列表
。要将项目附加到列表中,您可以执行a.append(B)
在将用户输入附加到数组之前,需要检查用户输入是否为
-1
,并在if
块中打印它,在else
块中附加它
A=[]
while True:
B = int(input("Input a continuous amount of integers"))
if B == -1:
print(A)
break
else:
A.append(B)
A.append(B)
而不是A=[B]
?A=[B]
-这不起作用。在这里,您正在创建一个包含一个元素的数组,但您的任务要求您“向数组中添加整数”。您需要使用append()
方法。此外,此消息毫无意义:input(“输入连续数量的整数”)
。它可能会说:输入一个整数:
或其他什么。以后请记住@BurningAlcohol在他的回答中所说的:an和a之间有区别(这里有一个列表,而不是数组
)“不要在数组中添加-1”
A=[]
while True:
B = int(input("Input a continuous amount of integers"))
if B == -1:
print(A)
break
else:
A.append(B)