1. 程式人生 > 其它 >第十三集:簡單的登入和註冊

第十三集:簡單的登入和註冊

names = []
pwds = []


def register():
num = input('請選擇:1.登入 2.註冊:')
with open('data.txt', 'r', encoding='utf-8') as fp:
ss = fp.readlines()
[names.append(i.strip().split(':')[0]) for i in ss]
users = {(i.strip().split(':')[0]): (i.strip().split(':')[1]) for i in ss}
if int(num) == 2:
print('開始註冊')
count = 3
while True:
name = input('請輸入使用者名稱:')
# 使用者名稱不能為空
if len(name) == 0:
print('使用者名稱不能為空請重新輸入')
continue
# 檢驗輸入的使用者名稱是否重複
if name in names:
print(f'{name}已存在,請重新輸入')
continue
else:
pwd = input('請輸入密碼:')
with open('data.txt', 'a+', encoding='utf-8') as fp:
fp.write(f'{name}:{pwd}\n')
break
if int(num) == 1:

while True:
count = 3
lockname =[]
name = input('請輸入使用者名稱:')
# 使用者名稱不能為空
if len(name) == 0:
print('使用者名稱不能為空請重新輸入')
continue
# 檢驗輸入的使用者名稱是否重複
if name not in names:
print(f'{name}不存在,請重新輸入')
continue

while True:
if count == 0:
print('沒有機會,賬戶已鎖定')
break
pwd = input('請輸入密碼:')
if pwd != users[name]:
print(f'密碼不正確,請重新輸入!還有{count - 1}機會')
count -= 1
if(count ==0):
lockname.append(name)
continue
else:
print('登入成功!')
break
continue

def login():
pass


register()
結果

請選擇:1.登入 2.註冊:1
請輸入使用者名稱:hhm
hhm不存在,請重新輸入
請輸入使用者名稱:d
d不存在,請重新輸入
請輸入使用者名稱:g
g不存在,請重新輸入
請輸入使用者名稱:d
d不存在,請重新輸入
請輸入使用者名稱:fds
fds不存在,請重新輸入
請輸入使用者名稱:cyz
請輸入密碼:12345
登入成功!
請輸入使用者名稱: