pytho写本实例要求编写代码,实现-一个基于面向对象思想的、具有添加好友、删除好友、展示好友、退出功能的好友管理系统。
时间: 2023-07-10 13:05:48 浏览: 156
好的,以下是一个简单的实现。
```python
class Friend:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class FriendManager:
def __init__(self):
self.friends = []
def add_friend(self, name, age, gender):
friend = Friend(name, age, gender)
self.friends.append(friend)
def remove_friend(self, name):
for friend in self.friends:
if friend.name == name:
self.friends.remove(friend)
return True
return False
def show_friends(self):
for friend in self.friends:
print(f"Name: {friend.name}, Age: {friend.age}, Gender: {friend.gender}")
def quit(self):
print("Goodbye!")
exit()
def run(self):
while True:
print("1. Add Friend")
print("2. Remove Friend")
print("3. Show Friends")
print("4. Quit")
choice = input("Enter your choice: ")
if choice == '1':
name = input("Enter name: ")
age = input("Enter age: ")
gender = input("Enter gender: ")
self.add_friend(name, age, gender)
elif choice == '2':
name = input("Enter name: ")
if not self.remove_friend(name):
print("Friend not found.")
elif choice == '3':
self.show_friends()
elif choice == '4':
self.quit()
else:
print("Invalid choice.")
if __name__ == '__main__':
friend_manager = FriendManager()
friend_manager.run()
```
该程序中,`Friend` 类表示一个朋友,包含名字、年龄和性别等属性。`FriendManager` 类表示好友管理器,包含添加、删除、展示和退出等功能。
在 `run` 方法中,通过循环等待用户输入,并根据用户选择执行相应的操作。用户可以输入相应的信息来添加、删除、展示好友,或者选择退出程序。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)