请添加图片描述

friends = {}

def add_friend():
    name = input("请输入添加的好友姓名: ")
    friends[name] = ""
    print("好友添加成功")

def delete_friend():
    name = input("请输入删除的好友姓名: ")
    if name in friends:
        del friends[name]
        print("删除成功")
    else:
        print("该好友不存在")

def modify_friend():
    name = input("请输入修改的好友姓名: ")
    if name in friends:
        new_name = input("请输入修改后的好友姓名: ")
        friends[new_name] = friends.pop(name)
        print("备注成功")
    else:
        print("该好友不存在")

def display_friends():
    choice = input("请选择展示类型 (1.展示所有好友 2.展示分组中的好友): ")
    if choice == '1':
        print("所有好友:")
        if len(friends) == 0:
            print("暂无好友")
        else:
            for friend in friends:
                print(friend)
    elif choice == '2':
        group_name = input("请输入分组名: ")
        print(f"分组 {group_name} 中的好友:")
        if group_name in friends.values():
            for friend, group in friends.items():
                if group == group_name:
                    print(friend)
        else:
            print("该分组不存在")
    else:
        print("无效的选择")

def friend_group():
    create_group = input("是否创建新的分组? (y/n): ")
    if create_group.lower() == 'y':
        group_name = input("请输入新的分组名: ")
        for friend in friends:
            print(friend)
            group = input(f"请输入好友 {friend} 所属的分组名: ")
            friends[friend] = group
        print("分组设置完成")
    elif create_group.lower() == 'n':
        print("取消创建分组")
    else:
        print("无效的选择")

def main():
    while True:
        print("**欢迎使用好友管理系统**")
        print("1:添加好友")
        print("2:删除好友")
        print("3:备注好友")
        print("4:展示好友")
        print("5:好友分组")
        print("6:退出")

        choice = input("请选择功能: ")
        if choice == '1':
            add_friend()
        elif choice == '2':
            delete_friend()
        elif choice == '3':
            modify_friend()
        elif choice == '4':
            display_friends()
        elif choice == '5':
            friend_group()
        elif choice == '6':
            print("感谢使用好友管理系统,再见!")
            break
        else:
            print("无效的选择")

main()

这段代码实现一个简单的好友管理系统程序运行后,会显示一个功能菜单用户可以根据提示选择相应的功能序号来执行操作。具体功能包括添加好友、删除好友、备注好友、展示好友和好友分组。

好友信息存储字典friends中,键为好友姓名,值为好友所属的分组名。用户可以通过相应的功能来操作

原文地址:https://blog.csdn.net/qq_39451322/article/details/130846778

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_30876.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注