import random,string nums = set() num = input('请输入要产生多少注:').strip() while len(nums)!=int(num): before = ' '.join(sorted(random.sample([str(nember).zfill(2)for nember in range(1,33)],5))) back = ' '.join(sorted(random.sample([str(n).zfill(2)for n in range(1,13)],2))) numbers = ('前区:%s 后区:%s '%(before,back)) nums.add(numbers) with open('大乐透.txt','a+',encoding='utf-8') as fw: fw.writelines(nums)