python 實現註冊程式

來源:互聯網
上載者:User

標籤:頻繁   image   不一致   list   har   inf   詳細   是否一致   pytho   

本文介紹用python實現一個類比註冊的程式,詳細需求如下:

#!/usr/bin/env python
# @Time : 2017/9/19 14:01
# @File : day2_register.py
# @Software: PyCharm Community Edition
# @Description: 註冊程式
# 寫一個註冊的程式,輸入username,密碼,密碼確認,輸入的帳號和密碼不可為空,兩次輸入密碼必須一致,使用者名稱不能重複,錯誤次數4次
# 思路:
# 1.登入使用者放在一個單獨的檔案裡(儲存已註冊的使用者)
# 2.引入登入使用者列表
# 3.定義一個字典,儲存傳入的註冊資訊:使用者名稱、密碼、確認密碼
# 4.使用者註冊提交,校正使用者名稱是否重複、兩次密碼是否一致
# 5.定義一個新的字典,儲存註冊成功的使用者資訊
# 6.把註冊成功的字典追加到使用者列表
# 7.把使用者列表重新儲存到檔案
# 8.註冊失敗達到4次,不再允許註冊

代碼主體如下:
import day2_userList
# 擷取登入使用者列表
userInfo = day2_userList.userInfo
# 定義一個空字典 用於存放使用者輸入資訊
userInput = {}
# 定義一個空字典 用於儲存註冊成功的使用者資訊
userSuccess = {}
# 定義一個註冊成功標誌位
flag = False
i = 0
while i < 4:
# 擷取使用者名稱和密碼
userInput["username"] = input("請輸入您的使用者名稱:")
userInput["pass"] = input("請輸入您的密碼:")
userInput["passConfirm"] = input("請再次確認您的密碼:")
# 初步校正
if not userInput["username"] or userInput["username"].isspace():
print("使用者名稱不可為空!")
i += 1
continue
if not userInput["pass"] or userInput["pass"].isspace():
print("密碼不可為空!")
i += 1
continue
if not userInput["passConfirm"] or userInput["pass"].isspace():
print("確認密碼不可為空")
i += 1
continue
if userInput["pass"] != userInput["passConfirm"]:
print("兩次輸入密碼不一致!")
i += 1
continue
# 校正使用者名稱是否重複
# 定義一個使用者標誌位
isHave = False
j = 0
for username in userInfo:
if username["username"] == userInput["username"]:
isHave = True
break
if isHave:
print("註冊失敗,您輸入的使用者名稱已被註冊!")
i += 1
continue
else:
print("恭喜你!註冊成功,快來學習吧!")
# 註冊成功,把使用者資訊放入userSuccess
userSuccess["username"] = userInput["username"]
userSuccess["pass"] = userInput["pass"]
userSuccess["status"] = 0
print("你本次註冊的使用者是:", userSuccess)
# 把userSuccess追加到userInfo
userInfo.append(userSuccess)
print("新的使用者列表:\n", userInfo)
# 把新的userInfo 存入檔案
file = open(r"C:\Users\shenping\PycharmProjects\Shenping_TEST\day_2\script\day2_userList.py", "w")
file.write("userInfo = " + str(userInfo))
file.close()
flag = True
break
if flag:
print("---------------------------------------------------")
print("成功註冊,程式退出!")
else:
print("----------------------------------------------------")
print("註冊失敗,失敗次數達4次,操作太頻繁,請稍後再試!")

使用者列表檔案day2_userList.py內容如下:

運行結果如下:

 註冊成功後使用者檔案清單內容:

 

python 實現註冊程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.