颜色输出模块:termcolor
http://pypi.python.org/pypi/termcolor

from termcolor import colored

Python颜色输出和random的学习

random 的 学习:

#!/usr/bin/env python
# -*- encoding: utf-8 -*
import random
from termcolor import colored

buytimes = 1

try:
    times = input('想买几注(默认一注): ')
except:
    times = 1

while  buytimes <= times:

    red     = map(str,range(1,34))
    blue    = map(str,range(1,17))
    random.shuffle(red)
    random.shuffle(blue)

    rball   = random.sample(red,6)
    bball   = random.sample(blue,1)

    strred  = ' '.join(rball)
    strblue = ' '.join(bball)
    
    buytimes += 1
    print colored(strred,'red') + ' ' + colored(strblue,'blue')

效果:
Python颜色输出和random的学习

知识点:
random.random()      :返回 0 <= n < 1的随机实数。
random.uniform(a,b) :返回 a <= n < b的随机实数。
random.randrange([start],stop,[step]) :返回range([start],stop,[step])的随机整数。
random.choice(seq)   :返回seq序列中的任意元素。
random.shuffle(seq)  :随机移位。
random.sample(seq,n) :从序列中取n个随机的元素。

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
  • 2021-05-21
  • 2021-08-06
  • 2021-10-07
  • 2021-10-12
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-02
  • 2021-07-12
  • 2022-03-04
  • 2021-09-27
  • 2021-07-29
相关资源
相似解决方案