【Python】学生通讯信息管理系统
ES2021年04月20日02:05:16

首先你得清楚,这玩意只是用来交作业的,你不可能真的用在项目里。可能比较啰嗦,但应该没bug。
源码下载

import json

def add():
 print('请输入要添加的学生通讯信息:')
 name = input('姓名:')
 with open('data.json', 'r') as f_r:
  if name in f_r.read():
   print('已存在'+name+'的通讯信息')
   main()
  else:
   gender = input('性别;')
   tel = input('号码:')
   data = [{'姓名': name, '性别': gender, '号码': tel}]
   json.dumps(data)
   result = str(data)
   with open('data.json', 'a') as f:
    f.write('{}\n'.format(result))
   print('已添加学生通讯信息')

def delete():
 name = input('请输入要删除通讯信息的学生姓名:')
 with open('data.json', 'r') as f_r:
  if name in f_r.read():
   with open('data.json', 'r') as f_r2:
    lines = f_r2.readlines()
   with open('data.json', 'w') as f_w:
    for line in lines:
     if name in line:
      print('已删除' + name + '的通讯信息')
      continue
     f_w.write(line)
  else:
   print('不存在' + name + '的通讯信息')

def update():
 name = input('请输入要修改通讯信息的学生姓名:')
 with open('data.json', 'r') as f_r:
  if name in f_r.read():
   with open('data.json', 'r') as f_r2:
    lines = f_r2.readlines()
   with open('data.json', 'w') as f_w:
    for line in lines:
     if name in line:
      continue
     f_w.write(line)
   print('请输入要修改后的学生通讯信息:')
   name = input('姓名:')
   gender = input('性别;')
   tel = input('号码:')
   data = [{'姓名': name, '性别': gender, '号码': tel}]
   json.dumps(data)
   result = str(data)
   with open('data.json', 'a') as f:
    f.write('{}\n'.format(result))
    print('已修改' + name + '的通讯信息')
  else:
   print('不存在' + name + '的通讯信息')

def find():
 name = input('请输入要查询通讯信息的学生姓名:')
 with open('data.json', 'r') as f_r:
  if name in f_r.read():
   with open('data.json', 'r') as f_r2:
    lines = f_r2.readlines()
    for line in lines:
     if name in line:
      print(line)
  else:
   print('不存在' + name + '的通讯信息')

def showall():
 with open('data.json', 'r') as f_r:
  data = f_r.read()
 print('以下是所有学生的通讯信息:\n'+data)

def main():
 print('--------------------')
 print('学生通讯管理系统 v1.0')
 print('1.添加学生通讯信息')
 print('2.删除学生通讯信息')
 print('3.修改学生通讯信息')
 print('4.查询学生通讯信息')
 print('5.获取所有学生通讯信息')
 print('6.退出系统')
 print('--------------------')
 choice = input('请输入选择的序号:')
 if choice == '1':
  add()
 elif choice == '2':
  delete()
 elif choice == '3':
  update()
 elif choice == '4':
  find()
 elif choice == '5':
  showall()
 elif choice == '6':
  print('已退出学生通讯管理系统')
 else:
  print('输入错误,请重新输入')
  main()

main()