jobkkk 发表于 2020-5-22 22:34:38

"""双色球的开奖模拟程序"""

"""双色球的开奖模拟程序"""
import random
i = 1
sample1 = []
sample2 = []
while i <=33:
    sample1.append(i)
    i = i + 1
i = 1
while i <=16:
    sample2.append(i)
    i = i + 1
print(random.sample(sample1, k=6))
print(random.sample(sample2, k=1))


老师的程序很简洁呀
"""双色球的开奖模拟程序"""

import random

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

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

这里用range(1,34)来完成列表的生成
>>> list(range(1,34))


同时在print("开奖结果是:", *red) 用了一个*号来把列表标识取消显示,不知我理解的对不对。

能做到上面这几点,那是对python的语法烂熟于心才能写出这样高效的程序来呀。

永恒的蓝色梦想 发表于 2020-5-22 23:22:21

这是基础

fendouxs 发表于 2020-5-23 08:23:32

{:10_257:}谢谢

東ニ大木 发表于 2022-7-27 11:27:08

我没看老师答案前,直接简单粗暴了一个,哈哈哈:
#模拟双色球
import random

a = random.randint(1,33)
b = random.randint(1,33)
c = random.randint(1,33)
d = random.randint(1,33)
e = random.randint(1,33)
f = random.randint(1,33)
g = random.randint(1,16)
#a != bcdef
while a == b:
      b = random.randint(1,33)
while a == c:
      c = random.randint(1,33)
while a == d:
      d = random.randint(1,33)
while a == e:
      e = random.randint(1,33)
while a == f:
      f = random.randint(1,33)
#b != cdef
while b == c:
      c = random.randint(1,33)
while b == d:
      d = random.randint(1,33)
while b == e:
      e = random.randint(1,33)
while b == f:
      f = random.randint(1,33)
#c != def
while c == d:
      d = random.randint(1,33)
while c == e:
      e = random.randint(1,33)
while c == f:
      f = random.randint(1,33)
#d != ef
while d == e:
      e = random.randint(1,33)
while d == f:
      f = random.randint(1,33)
#e != f
while e == f:
      f = random.randint(1,33)

print("开奖结果是:",a,b,c,d,e,f)
print("特别号码是:",g)

页: [1]
查看完整版本: """双色球的开奖模拟程序"""