python作業/練習/實戰:生成雙色球小程序
阿新 • • 發佈:2018-09-27
style pre words 生成 python NPU col 16px turn
作業要求:
每註投註號碼由6個紅色球號碼和1個藍色球號碼組成。紅色球號碼從1--33中選擇;藍色球號碼從1--16中選擇
代碼範例
import random all_red_ball = [str(i).zfill(2) for i in range(1, 34)] all_blue_ball = [str(i).zfill(2) for i in range(1, 17)] def gen_seq(): blue = random.choice(all_blue_ball) red = random.sample(all_red_ball,6) red= ‘ ‘.join(red) return ‘紅球:%s 籃球:%s‘%(red,blue) all_seq=set() num = int(input(‘請輸入要產生多少條雙色球:‘).strip()) while len(all_seq) != num: res = gen_seq()+‘\n‘ all_seq.add(res) with open(‘passwords.txt‘,‘w‘,encoding=‘utf-8‘) as fw: fw.writelines(all_seq)
python作業/練習/實戰:生成雙色球小程序