小练习 宠物管理系统
#coding=utf-8
PETS = [] # 宠物列表
def addPet():
petId = input("请输入宠物编号:")
petName = input("请输入宠物名称:")
petCategory = input("请输入宠物的种类:")
petPrice = input("请输入宠物的价格:")
pet = {"id":petId,"name":petName,"category":petCategory,"price":petPrice}
PETS.append(pet)
print("宠物添加成功")
def searchPetByName():
"""根据宠物名称来查找宠物"""
name = input("请输入宠物的名字")
for pets in PETS:
if pets["name"] == name:
text = "编号:{},名称:{},种类:{},价格:{}".format(
pets["id"],
pets["name"],
pets["category"],
pets["price"]
)
print(text)
else:
print("没有宠物的姓名是{0}".format(name))
def delPetById():
"""根据宠物Id删除宠物"""
Id = int(input("请输入宠物Id"))
for index in PETS:
if index == Id:
PETS.remove(index)
print("删除成功")
else:
print("删除失败,没有ID={0}的宠物".format(Id))
def searchEveryPet():
for pets in PETS:
text = "编号:{},名称:{},种类:{},价格:{}".format(
pets["id"],
pets["name"],
pets["category"],
pets["price"]
)
print(text)
if __name__ == '__main__':
while True:
print("欢迎使用宠物管理系统!")
print("1------------添加宠物")
print("2------------删除宠物")
print("3------------查找宠物")
print("4------------显示宠物")
print("5------------退出系统")
search = int(input("请选择相应的功能"))
if search == 1:
addPet()
elif search == 2:
delPetById()
elif search == 3:
searchPetByName()
elif search == 4:
searchEveryPet()
elif search == 5:
break
else:
print("请选择正确的功能")
PETS = [] # 宠物列表
def addPet():
petId = input("请输入宠物编号:")
petName = input("请输入宠物名称:")
petCategory = input("请输入宠物的种类:")
petPrice = input("请输入宠物的价格:")
pet = {"id":petId,"name":petName,"category":petCategory,"price":petPrice}
PETS.append(pet)
print("宠物添加成功")
def searchPetByName():
"""根据宠物名称来查找宠物"""
name = input("请输入宠物的名字")
for pets in PETS:
if pets["name"] == name:
text = "编号:{},名称:{},种类:{},价格:{}".format(
pets["id"],
pets["name"],
pets["category"],
pets["price"]
)
print(text)
else:
print("没有宠物的姓名是{0}".format(name))
def delPetById():
"""根据宠物Id删除宠物"""
Id = int(input("请输入宠物Id"))
for index in PETS:
if index == Id:
PETS.remove(index)
print("删除成功")
else:
print("删除失败,没有ID={0}的宠物".format(Id))
def searchEveryPet():
for pets in PETS:
text = "编号:{},名称:{},种类:{},价格:{}".format(
pets["id"],
pets["name"],
pets["category"],
pets["price"]
)
print(text)
if __name__ == '__main__':
while True:
print("欢迎使用宠物管理系统!")
print("1------------添加宠物")
print("2------------删除宠物")
print("3------------查找宠物")
print("4------------显示宠物")
print("5------------退出系统")
search = int(input("请选择相应的功能"))
if search == 1:
addPet()
elif search == 2:
delPetById()
elif search == 3:
searchPetByName()
elif search == 4:
searchEveryPet()
elif search == 5:
break
else:
print("请选择正确的功能")
# 这个是最基本的管理系统模板 其中没有将宠物数据缓存起来
# 有些地方也没有相应的判断 如宠物Id相同怎么解决?
# 小练习 熟悉语法 以及PEP8编码格式
# 初学Python 如有不足 请多多指教