介紹
這是我隨手寫的一個小程式,希望大家能從中學習到 串列 與 open() 函式,感受Python的魅力!
代碼瀏覽
點擊查看代碼
#獲取圖書
#從檔案中讀取圖書,并寫入串列
def getBook():
bookList =[]
f = open("book.txt","r")
tempList = f.readlines()
for temp in tempList:
temp = temp[:-1]
bookList.append(temp)
f.close()
return bookList
#保存圖書
#將添加的圖書從串列寫入檔案
def saveBook(bookList):
f = open("book.txt","w")
for temp in bookList:
f.write(temp+"\n")
f.close()
#添加圖書
#將圖書添加至串列
def addBook(book):
global books
books.append(book)
#洗掉圖書
#將圖書從串列中洗掉
def removeBook(book):
global books
if book in books:
books.remove(book)
else:
print("沒有找到",book,"這本書,")
#選單
print("""
1.添加圖書
2.洗掉圖書
3.展示所有圖書
4.保存并退出
""")
book = getBook()
while True:
x = input("請輸入功能鍵(1-4):")
if x == "1":
name = input("請輸入要添加的圖書名稱: ")
addBook(name)
elif x == "2":
tempBook = input("請輸入要洗掉的圖書名稱: ")
removeBook(tempBook)
elif x == "3":
print(books)
elif x == "4":
saveBook(books)
break
else:
print("輸入有誤,請重新輸入!")
下載
圖書管理系統(Python)
博客為本人的學習筆記,如有不足請指正
本文來自博客園,作者:淺末QM,轉載請注明原文鏈接:https://www.cnblogs.com/qm-zy/p/16514131.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/500173.html
標籤:其他
下一篇:C++多型
