import string, random all_num = [] def phone(): phone_number = input('请输入想要产生的条数:').strip() phone_number = int(phone_number) #转化为int for i in range(phone_number): p = random.sample(string.digits, 8) #随机生成8位数字 nums = '138' + ''.join(p)+' ' #拼接下 if nums not in all_num: #判断如果不在 all_num.append(nums) def weite_file(): with open('phones.txt', 'a+') as f: f.writelines(all_num) phone() weite_file()