python宠物系统_小练习 宠物管理系统
#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():
"""根據(jù)寵物名稱來查找寵物"""
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():
"""根據(jù)寵物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("歡迎使用寵物管理系統(tǒng)!")
print("1------------添加寵物")
print("2------------刪除寵物")
print("3------------查找寵物")
print("4------------顯示寵物")
print("5------------退出系統(tǒng)")
search?=?int(input("請選擇相應(yīng)的功能"))
if?search?==?1:
addPet()
elif?search?==?2:
delPetById()
elif?search?==?3:
searchPetByName()
elif?search?==?4:
searchEveryPet()
elif?search?==?5:
break
else:
print("請選擇正確的功能")
# 這個是最基本的管理系統(tǒng)模板 其中沒有將寵物數(shù)據(jù)緩存起來
# 有些地方也沒有相應(yīng)的判斷 如寵物Id相同怎么解決?
# 小練習(xí) 熟悉語法 以及PEP8編碼格式
# 初學(xué)Python 如有不足 請多多指教
總結(jié)
以上是生活随笔為你收集整理的python宠物系统_小练习 宠物管理系统的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux下ftp服务器的搭建
- 下一篇: [翻译] python Tutorial