pytho2
# 學(xué)生信息有姓名、性別、年齡、學(xué)號、班級。
# gai,沒有年齡,沒有學(xué)號,只有手機(jī)號
# 基本功能展示:
"""
============================
歡迎使用【學(xué)生信息管理系統(tǒng)】
? ? 1.顯示所有學(xué)生信息
? ? 2.新建學(xué)生信息
? ? 3.查詢學(xué)生信息
? ? 4.修改學(xué)生信息
? ? 5.刪除學(xué)生信息
? ? 0.退出系統(tǒng)
============================
"""
# 所有學(xué)生信息用一個列表模擬學(xué)生數(shù)據(jù)庫。
student_data = [
? ? {
? ? ? ? '姓名': '李明',
? ? ? ? '性別': '男',
? ? ? ? #'年齡': 18,
? ? ? ? '電話': 20200001,
? ? ? ? '班級': '2001班',
? ? },
? ? {
? ? ? ? '姓名': '張華',
? ? ? ? '性別': '男',
? ? ? ? #'年齡': 19,
? ? ? ? '電話': 20200002,
? ? ? ? '班級': '2002班',
? ? }
]
# 定義窗口函數(shù):show_window()
def show_window():
? ? print("""
============================
歡迎使用【學(xué)生信息管理系統(tǒng)】
? ? 1.顯示所有學(xué)生信息
? ? 2.新建學(xué)生信息
? ? 3.查詢學(xué)生信息
? ? 4.修改學(xué)生信息
? ? 5.刪除學(xué)生信息
? ? 0.退出系統(tǒng)
============================
""")
# 定義顯示所有學(xué)生信息的函數(shù):show_all()
# 由于學(xué)生信息數(shù)據(jù)為列表,所以需要用到for循環(huán)輸出
def show_all_stu():
? ? for student in student_data:
? ? ? ? print(student)
# 定義新建學(xué)生信息的函數(shù):create_stu()
def create_stu():
? ? name = input("請輸入學(xué)生姓名:")
? ? sex = input("請輸入學(xué)生性別:")
? ? #age = int(input("請輸入學(xué)生年齡:"))
? ? stu_id = int(input("請輸入學(xué)生電話:"))
? ? class_id = input("請輸入學(xué)生班級:")
? ? student = {
? ? ? ? '姓名': name,
? ? ? ? '性別': sex,
? ? ? ? #'年齡': age,
? ? ? ? '電話': stu_id,
? ? ? ? '班級': class_id
? ? }
? ? student_data.append(student)
# 定義查詢學(xué)生信息的函數(shù):find_stu()
def find_stu():
? ? name = input("請輸入要查詢的學(xué)生姓名:")
? ? for student in student_data:
? ? ? ? if student['姓名'] == name:
? ? ? ? ? ? print("該學(xué)生信息已查到,信息如下:",student)
? ? ? ? ? ? return student
? ? ? ? else:
? ? ? ? ? ? print("該學(xué)生不存在!")
? ? ? ? break
# 定義修改學(xué)生信息函數(shù):modify_stu()
def modify_stu():
? ? name = input("請輸入要修改信息的學(xué)生姓名:")
? ? for student in student_data:
? ? ? ? if student['姓名'] == name:
? ? ? ? ? ? num = int(input("請確認(rèn)需要修改該學(xué)生幾個信息:"))
? ? ? ? ? ? if num == 1:
? ? ? ? ? ? ? ? infor = input("請輸入需要修改的信息:")
? ? ? ? ? ? ? ? if infor == '姓名':
? ? ? ? ? ? ? ? ? ? student['姓名'] = input("請輸入修改后的學(xué)生姓名:")
? ? ? ? ? ? ? ? elif infor == '性別':
? ? ? ? ? ? ? ? ? ? student['性別'] = input("請輸入修改后的學(xué)生性別:")
? ? ? ? ? ? ? ? #elif infor == '年齡':
? ? ? ? ? ? ? ? ?#? ?student['年齡'] = int(input("請輸入修改后的學(xué)生年齡:"))
? ? ? ? ? ? ? ? elif infor == '電話':
? ? ? ? ? ? ? ? ? ? student['電話'] = input("請輸入修改后的學(xué)生電話:")
? ? ? ? ? ? ? ? elif infor == '班級':
? ? ? ? ? ? ? ? ? ? student['班級'] = input("請輸入修改后的學(xué)生班級:")
? ? ? ? ? ? ? ? print("該學(xué)生修改后的信息為:", student)
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? student['姓名'] = input("請輸入修改后的學(xué)生姓名:")
? ? ? ? ? ? ? ? student['性別'] = input("請輸入修改后的學(xué)生性別:")
? ? ? ? ? ? ? ?# student['年齡'] = int(input("請輸入修改后的學(xué)生年齡:"))
? ? ? ? ? ? ? ? student['電話'] = int(input("請輸入修改后的學(xué)生電話:"))
? ? ? ? ? ? ? ? student['班級'] = input("請輸入修改后的學(xué)生班級:")
? ? ? ? ? ? ? ? print("該學(xué)生修改后的信息為:",student)
? ? ? ? else:
? ? ? ? ? ? print("該學(xué)生不存在!")
? ? ? ? break
# 定義刪除學(xué)生信息的函數(shù):del_stu()
def del_stu():
? ? name = input("請輸入要刪除的學(xué)生姓名:")
? ? for student in student_data:
? ? ? ? if student['姓名'] == name:
? ? ? ? ? ? print("該學(xué)生信息已查到,信息如下:\n", student,"\n請確認(rèn)是否需要刪除該學(xué)生的信息?")
? ? ? ? ? ? infor = input("請輸入是or否:")
? ? ? ? ? ? if infor == "是":
? ? ? ? ? ? ? ? student_data.remove(student)
? ? ? ? ? ? elif infor == "否":
? ? ? ? ? ? ? ? break
? ? ? ? else:
? ? ? ? ? ? print("該學(xué)生不存在!")
? ? ? ? break
show_window()
# 用一個循環(huán)顯示運(yùn)行窗口:
while True:
? ? opreation = input("請輸入操作序號:")
? ? if opreation == "1":
? ? ? ? show_all_stu()
? ? elif opreation == "2":
? ? ? ? create_stu()
? ? elif opreation == "3":
? ? ? ? find_stu()
? ? elif opreation == "4":
? ? ? ? modify_stu()
? ? elif opreation == "5":
? ? ? ? del_stu()
? ? elif opreation == "0":
? ? ? ? print("謝謝使用,再見!")
? ? ? ? break
? ? else:
? ? ? ? print("請按照指定序號輸入!")
最后一題
正則表達(dá)式用法
import re
def show():
? ? print("="*66)
? ? print("= 注冊規(guī)則:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? =")
? ? print("=用戶名: 長度為6-10個字符,以漢字或字母或下劃線開頭? ? ? ? ? ? ? =")
? ? print("=密碼規(guī)則:長度為6-10個字符,必須以字母開頭,包含字母數(shù)字下劃線? =")
? ? print("=手機(jī)號規(guī)則:中國大陸手機(jī)號碼,以13、15、17開頭的手機(jī)號? ? ? ? ? ? ? ? ? ? ? ?=")
? ? print("郵箱基本格式為“名稱@域名”。mailbox")
? ? print("="*66)
num=1
while num<=3:
? ? show()
? ? p1=re.compile(r'[\u4e00-\u9fa5A-Za-z_]{6,10}')
? ? p2=re.compile(r'\b[A-Za-z][\w]{5,9}')
? ? p3=re.compile(r'[0-9]{11}')
? ? inname=input('請輸入注冊用戶名:')
? ? name=p1.search(inname)
? ? if name!=None:
? ? ? ? inpassword=input('請輸入注冊密碼:')
? ? ? ? password=p2.search(inpassword)
? ? ? ? if password!=None:
? ? ? ? ? ? inphone=input('請輸入手機(jī)號:')
? ? ? ? ? ? phone=p3.search(inphone)
? ? ? ? ? ? if password!=None:
? ? ? ? ? ? ? ? print("注冊成功!")
? ? ? ? ? ? ? ? print('用戶名:%s? 密碼:%s? 手機(jī)號:%s'%(inname,inpassword,inphone))
? ? ? ? ? ? ? ? break
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print("手機(jī)號格式錯誤")
? ? ? ? else:
? ? ? ? ? ? ? ? print("用戶密碼格式錯誤")
? ? else:
? ? ? ? print("用戶名格式錯誤!")
? ? ? ? print("錯誤%s次"%num)
? ? ? ? num+=1