python - Python数字数组程序
问题描述
我必须创建一个程序,用户输入询问数组中有多少个数字,您必须从数组中打印这些数字。
例子:
要添加到数组中的值:
12
[14, 64, 62, 21, 91, 25, 75, 86, 13, 87, 39, 48]
我不知道如何在一行中获取所有数字以及如何每次获取不同的数字。这是我到目前为止所拥有的:
import random
x = int(input("How many values to add to the array: "))
b = random.randint(1,99)
c = random.randint(1,99)
for i in range(x):
twainQuotes = [random.randint(b,c)]
for i in range(x-1):
print(twainQuotes)
解决方案
你可以使用列表理解:
if b > c:
b, c = c, b
twainQuotes = [random.randint(b, c) for _ in range(x)]
打印:_
print(*twainQuotes, sep='\n')
如果你想使用for
循环:
import random
x = int(input("How many values to add to the array: "))
b = random.randint(1,99)
c = random.randint(1,99)
if b > c:
b, c = c, b
twainQuotes = []
for i in range(x):
twainQuotes.append(random.randint(b,c))
for e in twainQuotes:
print(e)
示例输出(对于输入 = 2):
52
65
推荐阅读
- java - Scala - IntelliJ IDEA 错误:无法找到或加载主类
- usb - LUFA XInput 控制器端点输入/输出不工作
- swift - 有没有办法避免 Swift 保留/释放高性能代码?
- javascript - 在更新 forEach 数组时,在 forEach 内调用 AWS 开发工具包 S3
- wpf - 避免在折叠时读取 XAML 依赖属性
- python - 当我使用 pd.read_csv 读取大文件时出现 UnicodeDecodeError
- javascript - 如何在 Vue 模板内的 Moment.js 格式化程序上转义 HTML 字符串?
- javascript - Woocommerce 通过 JS 更改产品变体
- jenkins - Jenkins 远程执行器经常断开连接
- android - 为什么 Mockito 在参数正确时会抱怨 misusing.WrongTypeOfReturnValue?