代码如下:
class Friend(object):
name = None
phone = None
sex = None
def input_name(self):
self.name = input('输入名字:\t')
def input_phone(self):
self.phone = input('输入电话:\t')
def input_sex(self):
self.sex = input('输入性别:\t')
class FriendSystem(object):
friend_ls = []
# 添加学生信息
def add_friend(self, friend):
if isinstance(friend,Friend):
self.friend_ls.append(friend)
# 删除学生信息
def drop_friend(self):
# 根据名字进行删除
drop_name = input('需要删除的名字\t')
for friend in self.friend_ls:
if drop_name == friend.name:
self.friend_ls.remove(friend)
print('已删除{}\t'.format(drop_name))
return None
print('删除名字不在好友管理系统中')
return None
# 查询所有学生信息
def show_friends(self):
for friend in self.friend_ls:
print('名字:\t{} 电话:\t{} 性别:\t{}'.format(friend.name,
friend.phone, friend.sex))
# 修改学生信息
def change_remark(self):
change_name = input('需要修改的学生信息\t')
for friend in self.friend_ls:
if change_name == friend.name:
friend.name = input('新的学生姓名:\t')
friend.phone = input('新的学生电话:\t')
friend.sex = input('新的学生性别:\t')
return None
print('需要修改的学生不存在')
return None
@property
def names(self):
ls = []
for friend in self.friend_ls:
ls.append(friend.name)
return ls
# 写一个主函数
def system_main(self):
while True:
user_chioce = input('1:添加学生信息 2:删除学生信息 3:修改学生信息 4:查询所有学生信息 0:退出\n')
if user_chioce == '1':
friend = Friend()
friend.input_name()
friend.input_phone()
friend.input_sex()
self.add_friend(friend)
elif user_chioce == '2':
self.drop_friend()
elif user_chioce == '3':
self.change_remark()
elif user_chioce == '4':
self.show_friends()
elif user_chioce == '0':
break
else:
print('输入错误')
continue
friend_system = FriendSystem()
friend_system.system_main()