写一个菜单程序,能够对动物园的动物进行管理 动物类(id、type、color、sex、price、buyDate(入园时间)、isDead=false) 1、添加动物信息进动物园 2、注销一条动物信息(死亡或卖掉) 3、查询全部动物信息 4、查询某种颜色的动物信息 5、统计某种类型的动物信息 6、统计某种类型的动物的价值 7、修改某个动物的基本信息(输入id) 8、退出 注1:注销动物信息,可以通过改变动物的某一个特殊属性来控制。 比如,动物有个属性叫 isDead , 正常情况下是false,当注销的时候改成true,然后其他所有的统计和查询都只列出isDead属性为false的动物来,这样,看起来这个动物就不存在了。 注2:id是用来唯一标识动物信息的,区别动物主要就得靠ID了,所以添加动物信息的时候,id不允许重复 注3:动物信息是指类似下面的信息: ID:3、老虎、白色、雌、370000购买、2012-3-28入园
时间: 2024-02-04 22:03:15 浏览: 29
好的,以下是一个简单的菜单程序,可以对动物园的动物进行管理。代码如下:
```python
class Animal:
def __init__(self, id, animal_type, color, sex, price, buy_date, is_dead=False):
self.id = id
self.type = animal_type
self.color = color
self.sex = sex
self.price = price
self.buy_date = buy_date
self.is_dead = is_dead
animal_list = []
def add_animal():
id = int(input("请输入动物的ID:"))
for animal in animal_list:
if animal.id == id:
print("该ID已存在,请重新输入!")
return
animal_type = input("请输入动物的种类:")
color = input("请输入动物的颜色:")
sex = input("请输入动物的性别:")
price = float(input("请输入动物的价格:"))
buy_date = input("请输入动物的入园时间(格式:YYYY-MM-DD):")
animal = Animal(id, animal_type, color, sex, price, buy_date)
animal_list.append(animal)
print("添加成功!")
def delete_animal():
id = int(input("请输入要删除动物的ID:"))
for animal in animal_list:
if animal.id == id:
animal.is_dead = True
print("删除成功!")
return
print("未找到该动物!")
def show_all_animals():
print("ID\t种类\t颜色\t性别\t价格\t入园时间")
for animal in animal_list:
if animal.is_dead == False:
print("{}\t{}\t{}\t{}\t{}\t{}".format(animal.id, animal.type, animal.color, animal.sex, animal.price, animal.buy_date))
def show_animals_by_color():
color = input("请输入要查询的动物的颜色:")
print("ID\t种类\t颜色\t性别\t价格\t入园时间")
for animal in animal_list:
if animal.color == color and animal.is_dead == False:
print("{}\t{}\t{}\t{}\t{}\t{}".format(animal.id, animal.type, animal.color, animal.sex, animal.price, animal.buy_date))
def count_animals_by_type():
animal_type = input("请输入要统计的动物种类:")
count = 0
for animal in animal_list:
if animal.type == animal_type and animal.is_dead == False:
count += 1
print("共有{}只{}。".format(count, animal_type))
def count_animals_value_by_type():
animal_type = input("请输入要统计的动物种类:")
value = 0
for animal in animal_list:
if animal.type == animal_type and animal.is_dead == False:
value += animal.price
print("{}的总价值为{}元。".format(animal_type, value))
def modify_animal_info():
id = int(input("请输入要修改的动物的ID:"))
for animal in animal_list:
if animal.id == id:
animal_type = input("请输入动物的种类:")
color = input("请输入动物的颜色:")
sex = input("请输入动物的性别:")
price = float(input("请输入动物的价格:"))
buy_date = input("请输入动物的入园时间(格式:YYYY-MM-DD):")
animal.type = animal_type
animal.color = color
animal.sex = sex
animal.price = price
animal.buy_date = buy_date
print("修改成功!")
return
print("未找到该动物!")
while True:
print("1、添加动物信息进动物园")
print("2、注销一条动物信息(死亡或卖掉)")
print("3、查询全部动物信息")
print("4、查询某种颜色的动物信息")
print("5、统计某种类型的动物信息")
print("6、统计某种类型的动物的价值")
print("7、修改某个动物的基本信息(输入id)")
print("8、退出")
choice = input("请输入要执行的操作:")
if choice == "1":
add_animal()
elif choice == "2":
delete_animal()
elif choice == "3":
show_all_animals()
elif choice == "4":
show_animals_by_color()
elif choice == "5":
count_animals_by_type()
elif choice == "6":
count_animals_value_by_type()
elif choice == "7":
modify_animal_info()
elif choice == "8":
break
else:
print("输入有误,请重新输入!")
```
这个程序可以实现菜单中的所有功能,你可以根据需要进行修改和完善。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![](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)