Python: 练习作品-双色球随机组
代码说明
- 虽然头文件设定了utf-8,但没有中文,因为嫌麻烦。
这段代码是今天上班公交路上写的,回家路上完工,睡觉前测试修补完成。 - 代码编写工具,在手机上使用Qpython加完成编写和调试。
- 这段代码的目标是为了练习class语句的用法。
虽然学习了一些代码规范,比如逗号空格,运算和逻辑符空格,对齐规范,但是用print时,对print()用法很不习惯,也没有及时查资料,一会儿print(),一会儿print "",需要下次改进。
双色球随机数的对象用法:
- 创建对象 a = DoubleNumber()
默认初始化class属性的self.NumList为1~36的数组
也可以传参 a = DoubleNumber(30)建立一个1~30的数组。 - 随机抽取数字的方法 Out(),默认为从self.NumList数组里随机抽取7个数,并返回该数组结果。Out(10)为返回随机10个数的数组结果。
- 打印多组双色球的随机组合 Outs(),默认打印10组双色球随机组合。Outs(100)为100组。
- 直接执行py文件效果,为输入一个数量,输出该数量的双色球随机组合。
- import我没试过,结果未知。
如何运行它?保存为py文件后,
-
Windows – 电脑安装有python2.7,打开idle并执行py文件
-
Android – 请安装Qpython后运行py文件即可
-
Linux/Mac – 大部分自带python,如果版本正好是2.7,直接在终端运行py文件
#!/usr/bin/env python #coding=utf-8 """ * Practice : DoubleNumber * Parameter: DoubleNumber(Para1).Out(Para2) return a List(Para2) random from List(Para1) * DoubleNumber().Outs(Para3) return some List(7) from List(1~36) * Coder : AT * Date : 2016-04-12 * Blog : https://ghost.atibm.com/2016/04/12/python-lian-xi-shuang-se-qiu-sui-ji-zu/ """ import random class DoubleNumber: def __init__(self, NumMax=36): try: NumMax = int(NumMax) except: #print("Error Parameter !") #print(NumMax, "is not a INT number") #print("Start to use Default NumList 1~36") NumMax = 36 if NumMax > 0: self.NumMax = NumMax else: #print("Error Parameter!") #print(NumMax, "is too small...") #print("Start to use Default NumList 1~36 ") self.NumMax = 36 self.NumList = range(1, self.NumMax+1) def Out(self, NumMount=7): NumOutList = [] try: NumMount = int(NumMount) except: #print("Error Parameter !") #print(NumMount, "is not a INT !") #print("Start to use default NumMount 1 ") NumMount = 1 if NumMount > 1: if NumMount < self.NumMax: #print("for code out a NumMount List...to be continued !") NumOutList = random.sample(self.NumList, NumMount) elif NumMount > self.NumMax: #print("Error Parameter !\n") #print(NumMount, "is too big, out of NumMax !\n") #print("Start to set NumOutList as NumList ") NumOutList = self.NumList elif NumMount == self.NumMax: NumOutList = self.NumList else: #print("Error Parameter !") #print(NumMount, "<= 1 is too small !") #print("Start to use Default out NumMount 1") NumMount = 1 NumOutList = random.choice(self.NumList) NumOutList.sort() return NumOutList def Outs(self, OutMount=10): for i in range(OutMount): NumOutList = self.Out() print "No. %04d :"%(i+1), for j in NumOutList: print str(j).rjust(2,"0"), print ''' if __name__ == "__main__": while True: print("Start Class DoubleNumber Demo ....") print("Type a INT Number (NumMax) to Create a object of it ...") print("The NumMax is set source List lenth :") NumMax = raw_input() if NumMax == "at": break testDoubleNumber = DoubleNumber(NumMax) print("Type a INT Number (NumMount) to set Out List Lenth :") NumMount = raw_input() if NumMount == "at": break print testDoubleNumber.Out(NumMount) if raw_input("Enter to continue ......") == "at": break ''' if __name__ == "__main__": while True: try: OutMount = input("How many Random List you want :") except: print "Error!Please type a INT !" continue if OutMount > 0: testDoubleNumber = DoubleNumber() testDoubleNumber.Outs(OutMount) else: print "Error! Please type a INT > 0 :", raw_input() continue if raw_input("enter to continue(at for quit!)") == "at": break