1. 数据存到文件里的格式: 姓名、年龄、身份证、手机、学科
2. 确保手机、身份证唯一
3. 选学科时,给用户列出选项供选择
https://blog.csdn.net/m0_69606520/article/details/124373854
db_file ="student_data.db"
def validate_phone(num):
if not num.isdigit():
exit("phone umber must be digit")
if len(num)!=11:
exit("phone number length must be 11")
return True
def register_api():
stu_data = {}
print("welcome".center(50,"-"))
print("Please singn up!")
name =input("Name:").strip()
age =input("Age:").strip()
phone =input("Phone:").strip()
if phonein phone_list:
exit("this phone already registered,quit!")
validate_phone(phone)
id_num =input("ID:").strip()
if id_numin id_num_list:
exit("this id num already registered,quit!")
course_list = ["Phyton","Java","C++","Chinese","English"]
for index,coursein enumerate(course_list):
print(f"{index}.{course}")
selected_course =input("Choose the couse you prefer:")
if selected_course.isdigit():
selected_course =int(selected_course)
if selected_course >=0 and selected_course <=len(course_list):
picked_course = course_list[selected_course]
else:
exit("illegal input")
else:
exit("illegal input")
stu_data["name"] = name
stu_data["age"] = age
stu_data["phone"] = phone
stu_data["id_num"] = id_num
stu_data["course"] = picked_course
return stu_data
def commit_to_db(filename,stu_data):
"""
:param filename:学员数据库文件
:param stu_data: 单个学员数据的dict
:return:
"""
f =open(filename,"a")
row =f"{stu_data['name']},{stu_data['age']},{stu_data['phone']},{stu_data['id_num']},{stu_data['course']}"
f.write(row)
f.close()
def load_validated_data(filename):
f =open(filename)
phone_list = []
id_num_list = []
for linein f:
line = line.split(",")
phone = line[2]
id_num = line[3]
phone_list.append(phone)
id_num_list.append(id_num)
return phone_list,id_num_list
phone_list,id_num_list = load_validated_data(db_file)
student_data = register_api()
print(student_data)
commit_to_db(db_file,student_data)
网友评论