实现一个基于面向对象思想的、具有添加好友、删除好友、展示好友、退出功能的好友管理系统。python
时间: 2023-07-10 12:12:56 浏览: 243
好的,下面是一个用Python实现的好友管理系统:
```
class Friend:
def __init__(self, name, contact):
self.name = name
self.contact = contact
class FriendManager:
def __init__(self):
self.friends = []
def add_friend(self, name, contact):
friend = Friend(name, contact)
self.friends.append(friend)
def remove_friend(self, name):
for friend in self.friends:
if friend.name == name:
self.friends.remove(friend)
print("{} has been removed from your friend list.".format(name))
return
print("{} is not in your friend list.".format(name))
def show_friends(self):
if not self.friends:
print("You haven't added any friends yet.")
else:
print("Your friend list:")
for friend in self.friends:
print("{}: {}".format(friend.name, friend.contact))
def exit(self):
print("Goodbye!")
exit()
if __name__ == "__main__":
fm = FriendManager()
while True:
print("What would you like to do?")
print("1. Add a friend")
print("2. Remove a friend")
print("3. Show your friend list")
print("4. Exit")
choice = input("> ")
if choice == "1":
name = input("Enter your friend's name: ")
contact = input("Enter your friend's contact: ")
fm.add_friend(name, contact)
elif choice == "2":
name = input("Enter your friend's name: ")
fm.remove_friend(name)
elif choice == "3":
fm.show_friends()
elif choice == "4":
fm.exit()
else:
print("Invalid choice. Please try again.")
```
在这个程序中,我们定义了一个Friend类来表示好友,它包含了两个属性:姓名和联系方式。我们还定义了一个FriendManager类来管理好友列表,它包含了添加好友、删除好友、展示好友和退出功能。
在主程序中,我们创建了一个FriendManager对象fm,并进入一个循环,等待用户输入操作。用户可以选择添加好友、删除好友、展示好友或退出程序。根据用户的选择,我们调用FriendManager类中相应的方法来执行相应的操作。
希望这个程序能够满足你的需求!
阅读全文