jackz6661 发表于 2022-2-26 15:58:49

双色球程序为什么显示中括号?

程序:
import random


result = random.sample(range(1, 34), k=6)
special = random.sample(range(1, 17), k=1)
print("result:" , result)
print("special:" , special)

red = random.sample(range(1, 34), 6)
blue = random.randint(1, 16)

print("开奖结果是:", *red)
print("特别号码是:", blue)

上面是我写的,下面是答案

运行结果:
result:
special:
开奖结果是: 13 18 33 1 5 25
特别号码是: 9

为什么答案里的结果没有中括号呢?

isdkz 发表于 2022-2-26 16:02:06

因为 sample 得出来的是列表,你直接打印列表了,你可以加个 * 把列表解包成多个位置参数:
import random


result = random.sample(range(1, 34), k=6)
special = random.sample(range(1, 17), k=1)
print("result:" , *result)   # 加个 *
print("special:" , *special)# 加个 *

jackz6661 发表于 2022-2-26 16:08:19

isdkz 发表于 2022-2-26 16:02
因为 sample 得出来的是列表,你直接打印列表了,你可以加个 * 把列表解包成多个位置参数:

感谢!
页: [1]
查看完整版本: 双色球程序为什么显示中括号?