用列表實現一個簡單的圖書管理系統 python


#coding=utf-8

book_list=[]  #圖書館所有書

unborrowed_book=[]  #可借閱的書

borrowed_book=[]  #已經借出去的書

 

def add():  #添加書

    global book_list

    global unborrowed_book

    book_name=raw_input(u"請輸入要添加的書名:".encode("gbk"))

    book_list.append(book_name)

    unborrowed_book.append(book_name)

    print u"添加書成功"

 

def borrow_book():  #借書

    global unborrowed_book

    global borrowed_book

    while 1:

        book_name=raw_input(u"請輸入要借的書名:".encode("gbk"))

        if book_name in unborrowed_book:

            unborrowed_book.remove(book_name)        

            borrowed_book.append(book_name)

            print u"借書成功"

            break

        else:

            print u"抱歉,您要借的書不存在!"

            continue_back=raw_input(u"繼續借書請輸入:c 返回主菜單請輸入:b ".encode("gbk"))

            if continue_back.lower()=="c":

                continue

            elif continue_back.lower()=="b":

                break

            else:

                print u"輸入數據無效"            

 

def return_book():  #還書

    global borrowed_book

    global unborrowed_book

    while 1:

        book_name=raw_input(u"請輸入要還的書名:".encode("gbk"))

        if book_name in borrowed_book:

            borrowed_book.remove(book_name)

            unborrowed_book.append(book_name)

            print u"還書成功"

            break

        else:

            print u"您輸入的書名不是此圖書館的"

            continue_back=raw_input(u"繼續還書請輸入:c 返回主菜單請輸入:b ".encode("gbk"))

            if continue_back.lower()=="c":

                continue

            elif continue_back.lower()=="b":

                break

            else:

                print u"輸入數據無效"    

 

menu_info='''

添加書--請輸入:1

借書--請輸入:2

查看可借閱的書--請輸入:3

查看借出去的書--請輸入:4

還書--請輸入:5

查看所有書--請輸入:6

退出--請輸入:q

'''

import sys

print menu_info.decode("utf-8")

 

while True:    

    command=raw_input(u"請輸入您要做的操作選項:".encode("gbk"))

    if command=="1":

        add()

    elif command=="2":

        borrow_book()

    elif command=="3":

        if unborrowed_book==[]:

            print u"抱歉,沒有可以借的書了!"

        else:

            print u"可借閱的書:",unborrowed_book    

    elif command=="4":

        if borrowed_book==[]:

            print u"沒有借出去的書!"

        else:

            print u"借出去的書:",borrowed_book

    elif command=="5":

        return_book()

    elif command=="6":

        if book_list==[]:

            print u"圖書館還沒有書,趕快去添加吧!"

        else:

            print u"圖書館所有的書:",book_list

    elif command=="q":

        sys.exit()

    else:

        print u"輸入數據無效!"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM