簡單實現乙個可以註冊 登入 登出的系統

2021-09-22 02:41:21 字數 3859 閱讀 7350

from user_class.user import admin

if __name__ == '__main__':

user = admin()

while (true):

number = input("1:註冊,2:登入,3:退出登入:")

is_or_no = number.isdigit()

if not is_or_no:

print('請輸入數字!!')

else:

int_number = int(number)

if int_number == 1:

print('-------------------註冊-------------------')

user.regist()

elif int_number == 2:

print('-------------------登入-------------------')

user.login()

elif int_number == 3:

print('-------------------退出-------------------')

user.logout()

else:

print('請輸入正確的數字!!')

user.py

# 自定義多個異常類並繼承exception

class myexception(exception):

def __init__(self, *args):

self.args = args

class phone_no(myexception):

def __init__(self, args=('請輸入11位手機號!!',)):

self.args = args

class pwd_no(myexception):

def __init__(self, args=('請輸入六位數字的密碼!!',)):

self.args = args

class unequal(myexception):

def __init__(self, args=('請輸入相同的密碼!!',)):

self.args = args

class user_no(myexception):

def __init__(self, args=('該使用者不存在!!',)):

self.args = args

class pwd_wrong(myexception):

def __init__(self, args=('密碼錯誤!!',)):

self.args = args

# 主要程式

class admin:

# 使用者資訊

def __init__(self):

self.userfile = 'users.txt'

# 登入

def login(self):

phone = input('請輸入手機號:\n')

with open(self.userfile, 'r', encoding='utf-8') as user_file:

for line in user_file:

line = line.strip()

line_list = line.split(" ")

if line_list[0] == phone:

password = input("請輸入密碼:\n")

# 使用者手機和密碼與資料對應

if line_list[0] == phone and line_list[1] == password:

print("歡迎登入!")

break

elif line_list[0] == phone and line_list[1] != password:

# 密碼錯誤

try:

# 丟擲異常

raise pwd_wrong()

except pwd_wrong as msg:

# 提示錯誤資訊

print(msg)

break

elif line_list[0] != phone:

# 使用者手機號未註冊

try:

raise user_no()

except user_no as msg:

print(msg)

break

# 註冊

def regist(self):

phone = input('請輸入11位手機手機號:\n')

if len(phone) != 11:

# 不是11位手機號

try:

raise phone_no()

except phone_no as msg:

print(msg)

else:

with open(self.userfile, 'r', encoding='utf-8') as user_file:

for line in user_file:

line = line.strip()

line_list = line.split(" ")

if line_list[0] == phone:

print('該使用者已經註冊,請直接登入!')

break

else:

pwd1 = input("請輸入6位密碼:\n")

if len(pwd1) != 6:

# 不是6位密碼

try:

raise pwd_no()

except pwd_no as msg:

print(msg)

break

pwd2 = input("請再次輸入6位密碼:\n")

if len(pwd2) != 6:

if len(pwd1) != 6:

# 不是6位密碼

try:

raise pwd_no()

except pwd_no as msg:

print(msg)

break

if pwd1 == pwd2:

with open(self.userfile, 'a', encoding='utf-8') as f:

temp = phone + " " + pwd1 + '\n'

f.write(temp)

print(phone + '註冊成功!')

break

else:

# 兩次輸入的密碼不一致

try:

raise unequal()

except unequal as msg:

print(msg)

break

# 退出登入

def logout(self):

phone = input("請輸入需要退出的使用者手機號:\n")

with open(self.userfile, 'r', encoding='utf-8') as user_file:

for line in user_file:

line = line.strip()

line_list = line.split(" ")

if line_list[0] == phone:

print("{}退出登入".format(phone))

break

else:

# 該使用者不存在!!

try:

raise user_no()

except user_no as msg:

print(msg)

break

python實現乙個簡單的登入註冊Demo

coding utf 8 created on fri feb 2 16 14 31 2018 author administrator 乙個簡單的登入註冊demo def showmessage print 新建使用者 鍵入n n print 登入帳號 鍵入e e print 退出程式 鍵入q q...

乙個簡單的註冊 登入程式

乙個簡單的註冊。登入程式。已實現反射,使用者只需要輸入數字即可選擇功能。可保留註冊資訊。定義了乙個只有退出功能的函式。class user def init self,name,pwd self.name name self.pwd pwd class authentic def init self...

PHP實現乙個簡陋的註冊登入頁面

php實現乙個簡陋的註冊登入頁面 今天來水一篇沒有 用的 滑稽臉,簡陋臃腫考慮不全,各位大佬輕噴,還望不吝賜教。首先考慮了一下需要至少四個頁面 register.html register.php login.html login.php。register.html是這麼寫的 register.ph...