一、寫一個登錄注冊程序,兩個py文件,一個txt文件,用戶名和密碼存在文件中。
freg = open('users.txt', 'a+', encoding='utf-8')
freg.seek(0)
#定義存放用戶名,密碼的 字典
users_info = {}
#逐行讀取用戶名,密碼,並存儲到字典中
for line in freg.readlines():
users = line.strip().split(',')
users_info[users[0]]=users[1]
# print(users_info)
#注冊
while True:
username = input('請輸入用戶名:').strip()
password = input('請輸入密碼:').strip()
cpasswd = input('請確認密碼:').strip()
if username and password and cpasswd:
if username in users_info.keys():
print('用戶名已存在,請重新注冊')
continue
else:
if password != cpasswd:
print('兩次輸入密碼不一致,請重新注冊!')
continue
else:
freg.write(username+','+password + '\n')
print('%s,注冊成功!' % username)
break
else:
print('用戶名密碼不允許為空')
freg.close()
#登錄
freg = open('users.txt','r')
freg.seek(0)
users_info={}
for line in freg.readlines():
users = line.strip().split(',')
users_info[users[0]] = users[1]
freg.close()
for i in range(3):
username = input('請輸入用戶名:')
password = input('請輸入密碼:')
if username and password:
if username in users_info:
pwd = users_info[username]
if password != pwd:
print('密碼輸入錯誤')
else:
print('%s,歡迎您!'%username)
break
else:
print('用戶名不存在!')
else:
print('用戶名密碼不允許為空!')
else:
print('輸入錯誤次數過多!稍后再試')
二、寫一個生成隨機密碼的程序,隨機密碼存放在文件中。
#1、輸入要生成密碼的條數:
#2、生成8位隨機密碼,密碼包括數字,大寫字母和小寫字母
import random,string
fpwd = open('pwds.txt','a+')
fpwd.seek(0)
counts = input('請輸入密碼條數:')
for i in range(int(counts)):
num = random.sample(string.digits,2)
sletter = random.sample(string.ascii_lowercase,2)
cletter = random.sample(string.ascii_uppercase,2)
chars = random.sample(string.ascii_letters+string.digits,2)
pwd = num+sletter+cletter+chars
pwds = ''.join(pwd)+'\n'
fpwd.writelines(pwds)