python - 如何将用户输入的整数添加到数组中?
问题描述
- 声明一个空数组
- 让用户一遍又一遍地向这个数组添加整数
- 当用户输入 -1 时停止
- 不要在数组中添加 -1
- 打印数组
到目前为止,我的代码如下:
A=[]
while True:
B = int(input("Input a continuous amount of integers"))
A = [B]
print(A)
if B == -1:
break
else:
continue
解决方案
在 Python 中,我们将 this[]
数据类型称为list
. 要将项目附加到列表中,您可以执行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)
推荐阅读
- r - 如何将许多 vennplots 放在一张幻灯片/情节中
- java - 当我将数据从 Jsp 提交到 spring 控制器时 @ModelAttribute 为空
- ios - SwiftUI 中的多级列表
- android - 获取 Android 资源链接失败
- html - 无法单击某个元素,因为其他元素会遮挡它
- ios - onTapGesture 后偏移量不会重置 - SwiftUI
- c# - Sortedlist/Dictionary 在索引处修改键的值
- mysql - HQL 自动增量 INSERT - SELECT:“操作数应包含 1 列”
- android - 在 android kotlin 中以编程方式创建联系人并将其共享为 v-card/vcf 文件
- python - url {% url employees:check_matricule %} 没有在 forms.py 中运行