python實現超市管理系統(後臺管理)
阿新 • • 發佈:2020-01-09
本文例項為大家分享了python實現超市管理系統的具體程式碼,供大家參考,具體內容如下
這個相比上個程式簡單很多,首先他沒有太過複雜的邏輯關係,它的邏輯線條很清晰,你能很清楚的知道要做什麼,只要往自己想的方面實現就行,程式碼如下
# coding: utf-8 # 定義倉庫 repository = dict() # 定義購物清單物件 shop_list = [] # 定義倉庫裡商品數量 shangpin = [["1000001","你好世界",88.0,10],\ ["1000002","瘋狂python講義",69.0,12],\ ["1000003","康復手冊",59.0,188],\ ["1000004","瘋狂Java講義",109.0,56],\ ["1000005","瘋狂Android講義",108.0,100],\ ["1000006","世界起源",77.0,122]] # 定義一個函式來初始化商品 def init_repository(): # 遍歷商品生成倉庫dict字典 for i in range(len(shangpin)) : repository[shangpin[i][0]] = shangpin[i] #顯示超市的商品清單,就是遍歷代表倉庫的dict字典 def show_goods(): print("歡迎來到 哼嘿哈嘿樂園") print('哈嘿樂園的商品清單:') print("%13s%40s%10s%10s" % ("條碼","商品名稱","單價","數量")) # 遍歷repository的所有value來顯示商品清單 for s in repository.values(): s = tuple(s) print("%15s%40s%12s%12s" % s) # 顯示購物清單,就是遍歷代表購物清單的list列表 def show_list(): print("=" * 100) # 如果清單不為空的時候,輸出清單的內容 if not shop_list: print("還未購買商品") else: title = "%-5s|%15s|%40s|%10s|%4s|%10s" % \ ("ID","條碼","數量","小計") print(title) print("-" * 100) # 記錄總計的價錢 sum = 0 # 遍歷代表購物清單的list列表 for i,item in enumerate(shop_list,start=1): # 轉換id為索引加1 id = i # 獲取該購物項的第1個元素:商品條碼 code = item[0] # 獲取商品條碼讀取商品,再獲取商品的名稱 name = repository[code][1] # 獲取商品條碼讀取商品,再獲取商品的單價 price = repository[code][2] # 獲取該購物項的第2個元素:商品數量 number = item[1] # 小計 amount = price * number # 計算總計 sum = sum + amount line = "%-5s|%17s|%40s|%12s|%6s|%12s" % \ (id,code,name,price,number,amount) print( line ) print("-" * 100) print(" 總計: ",sum) print("=" * 100) # 新增購買商品,就是向代表使用者購物清單的list列表中新增一項。 def add(): # 等待輸入條碼 code = input("請輸入商品的條碼:\n") # 沒有找到對應的商品,條碼錯誤 if code not in repository: print("條碼錯誤,請重新輸入") return # 根據條碼找商品 goods = repository[code] # 等待輸入數量 number = input("請輸入購買數量:\n") # 把商品和購買數量封裝成list後加入購物清單 shop_list.append([code,int(number)]) # 修改購買商品的數量,就是修改代表使用者購物清單的list列表的元素 def edit(): id = input("請輸入要修改的購物明細項的ID:\n") # id減1得到購物明細項的索引 index = int(id) - 1 # 根據索引獲取某個購物明細項 item = shop_list[index] # 提示輸入新的購買數量 number = input("請輸入新的購買數量:\n") # 修改item裡面的number item[1] = int(number) # 刪除購買的商品明細項,就是刪除代表使用者購物清單的list列表的一個元素。 def delete(): id = input("請輸入要刪除的購物明細項的ID: ") index = int(id) - 1 # 直接根據索引從清單裡面刪除掉購物明細項 del shop_list[index] def payment(): # 先列印清單 show_list() print('\n' * 3) print("歡迎下次光臨") # 退出程式 import os os._exit(0) # 後臺新增商品函式 def adds(): # 獲取要新增的商品資訊 a = input("請輸入商品條碼:") b = input('請輸入商品名稱:') c = input('請輸入商品單價:') d = input('請輸入商品數量:') # 新增到商品列表 shangpin.append([a,b,c,d]) # 重新列印商品清單 init_repository() show_goods() # 後天修改商品屬性函式 def edits(): a = input("請輸入商品條碼:") # 獲取此商品條碼的新的值 if a in repository.keys(): e = input("請輸入修改後商品名字:") f = input("請輸入修改後商品單價:") g = input("請輸入修改後商品數量:") repository.update({a:[a,e,f,g]}) print(repository[a]) show_goods() else: print('輸入條碼有誤') def deletes(): h = input('請輸入您要下架商品條碼:') # 直接根據條碼從倉庫裡面刪除掉此商品 repository.pop(h) show_goods() # 重新列印商品清單 def show_good(): show_goods() # 後臺支援的操作 cmd_dicts = {'a': adds,'e': edits,'d': deletes,'s': show_good,'q': quit} def root(): # 先列印清單 show_goods() print("歡迎進入超市貨品管理平臺") print("=" * 100) while True: cmds = input("後臺操作指令: \n" + " 新增商品(a) 修改商品(e) 刪除商品(d) 全部商品(s) 退出(q)\n") if cmds == 'q' : return elif cmds not in cmd_dicts: print("好好玩,行嗎!") else: cmd_dicts[cmds]() # 使用者所支援的操作 cmd_dict = {'a': add,'e': edit,'d': delete,'p': payment,'s': show_goods,'r': root } # 初始倉庫並展示 init_repository() show_goods() # 顯示命令提示 def show_command(): # 等待命令 cmd = input("使用者操作指令: \n" + " 新增(a) 修改(e) 刪除(d) 結算(p) 超市商品(s) 後臺管理(r)\n") # 如果使用者輸入的字元沒有對應的命令 if cmd not in cmd_dict: print("不要玩,好不好!") else: cmd_dict[cmd]() # 顯示清單和操作命令提示 while True: show_list() show_command()
結語:
新增了後臺處理,但是程式碼還是很不完善,還有很多問題存在,這只是大概的思路,希望能給小夥伴們點啟發
小夥伴們如果有不清楚的地方請留言,我們共同進步
感謝:
本文原始碼參考瘋狂python講義
更多學習資料請關注專題《管理系統開發》。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。