import random user_sl=input('输入随机生成的条数') username=[] for i in range(int(user_sl)): kt='zhbr,123456' kt_str=kt.split(',') u=kt_str[0] p=kt_str[1] zj=str(random.randint(0,9999)) a=zj.zfill(4) user=u+a if user not in username: username.append(user + ',' + p + ' ') open('username.txt','w').writelines(username)