一个随机产生双色球的小程序说说random库的使用

import random red=[] #双色球红球 random.randint(1,33) blue=[] #双色球蓝球 random.randint(1,16) strin=input("请输入红球与蓝球个数,用‘+’隔开(如:7+2):") #获得用户想打的票的球的个数 print(strin) if strin=="": print("输入有误,下次再来吧!") else: if "+"in strin and len(strin.split("+"))==2: if (strin.split("+"))[0].isdigit() and (strin.split("+"))[1].isdigit(): #这里拿到我们想要的输入信息了,接着往下走 num_r=int(strin.split("+")[0]) #红球个数 num_b=int(strin.split("+")[1]) #蓝球个数 if (num_r>=6 and num_r<=15) and (num_b>=1 and num_b<=16): # 产生红球 i = 0 while i < num_r: temp = random.randint(1, 33) if temp not in red: red.append(temp) i += 1 else: continue # 产生蓝球 j = 0 while j < num_b: temp2 = random.randint(1, 16) if temp2 not in blue: blue.append(temp2) j += 1 else: continue red.sort() blue.sort() else: print("输入红球或蓝球个数有误,下次再来吧!") else: print("输入有误,下次再来吧!") else: print("输入有误,下次再来吧!") def formystr(l): #l为传入的列表,n为多少个数 re="" for i in l: re+=str(i) if i!=l[-1]: re+="+" return re print(formystr(red)+"*"+formystr(blue)) “”“ 输出结果: 请输入红球与蓝球个数,用‘+’隔开(如:7+2):7+3 7+3 3+8+12+17+21+23+24*1+2+8 """

猜你喜欢

转载自www.cnblogs.com/yiyea/p/11441731.html