08->python3註冊登錄(文件操作)
阿新 • • 發佈:2019-05-04
註冊登錄 三次 [] \n 寫入 div input %s utf
name = input("請註冊用戶:") password = input("請註冊密碼:") with open(file="user", mode=‘w‘, encoding=‘utf-8‘) as f: f.write(‘{}\n{}‘.format(name, password)) # 將用戶、密碼寫入文件 print("恭喜%s,註冊成功!" % name) count = 0 li = [] while count < 3: # 三次輸入機會 _name = input("請輸入用戶:") _password = input("請輸入密碼:") with open(file=‘user‘, mode=‘r+‘, encoding=‘utf-8‘) as f: # 讀取文件 for line in f: #li.append(line) if li[0].strip() == _name and li[1].strip() == _password: print("登錄成功!") break else: print("請重新輸入!")if count == 2: print("笨蛋,回家養豬去吧!!!!!") count += 1
# 改良
name = input("請註冊用戶:") password = input("請註冊密碼:") with open(file="{}".format(name), mode=‘w‘, encoding=‘utf-8‘) as f: f.write(‘{}\n{}‘.format(name, password)) print("恭喜%s,註冊成功!" % name) count = 0 # li = [] whilecount < 3: _name = input("請輸入用戶:") _password = input("請輸入密碼:") with open(file="{}".format(name), mode=‘r+‘, encoding=‘utf-8‘) as f: # for line in f: # li.append(line) li = f.readlines() if li[0].strip() == _name and li[1].strip() == _password: print("登錄成功!") break else: print("請重新輸入!") if count == 2: print("笨蛋,回家養豬去吧!!!!!") count += 1
過程
1)用戶交互-註冊用戶、密碼
2)寫入文件
3)while循環,3次機會
4)用戶交互-輸入用戶、密碼
5)讀文件
6)str比較
08->python3註冊登錄(文件操作)