python超市管理系统_控制台超市系统(Python)
#定義倉庫
repos = dict()
#定義購物清單對象
shop_list = []
#定義函數來初始化商品
def init_repository():
goods1 = ("1001","鼠標",45)
goods2 = ("1002","鍵盤",78)
goods3 = ("1003","毛筆字帖",21.5)
goods4 = ("1004","算法競賽",89)
goods5 = ("1005","聲律啟蒙",35)
goods6 = ("1006","Python爬蟲",118)
#把商品放入dict中(入庫),條碼作為key
repos[goods1[0]] = goods1
repos[goods2[0]] = goods2
repos[goods3[0]] = goods3
repos[goods4[0]] = goods4
repos[goods5[0]] = goods5
repos[goods6[0]] = goods6
#定義函數,顯示超市商品清單,即遍歷字典dict
def show_goods():
print('歡迎光臨 獨狼超市')
print('超市的商品清單為:')
print("%13s%30s%12s" % ("條碼","商品名稱","單 價"))
#遍歷repos中所有value來顯示商品清單
for goods in repos.values():
print("%15s%30s%12s" % goods)
#定義函數,顯示購物清單(遍歷shop_list列表)
def show_list():
print("="*100)
#如果清單不為空,則輸出清單的內容
if not shop_list:
print("還未購買商品")
else:
title = "%-5s | %15s | %30s | %10s | %4s | %10s" % ("ID","條碼","商品名稱","單價","數量","小計")
print(title)
print("-"*100)
#記錄總計的價錢
sum = 0
#遍歷購物清單shop_list
for i,item in enumerate(shop_list):
id = i + 1 #轉換id為索引加1
code = item[0] #獲取商品條碼
name = repos[code][1] #獲取商品名稱
price = repos[code][2] #獲取商品單價
number = item[1] #獲取商品數量
amount = price * number #小計
sum = sum + amount #總計
line = "%-5s | %17s | %30s | %12s | %6s | %12s "% (id,code,name,price,number,amount)
print(line)
print("-"*100)
print(" 總計:",sum)
print("="*100)
#定義函數,添加購買的商品(向shop_list中添加項)
def add():
code = input("請輸入商品的條碼:\n")
#若沒有找到對應的商品,則輸出條目錯誤
if code not in repos:
print("條碼錯誤,請重新輸入!")
return
goods = repos[code] #根據條碼找商品
number = input("請輸入購買的數量:\n")
shop_list.append([code,int(number)]) #把商品和購買數量封裝成list后加入到購物清單中
#定義函數,修改購買商品的數量
def edit():
id = input("請輸入要修改的購物單的ID:\n")
index = int(id)-1 #id減1得到購物單的索引
item = shop_list[index] #根據索引獲取購物清單
number = input("請輸入新的購買數量:\n")
item[1] = number #修改item里面的number
#定義函數,刪除購物清單項
def delete():
id = input("請輸入要刪除的購物清單的ID:\n")
index = int(id) - 1
del shop_list[index]
#退出系統函數
def payment():
show_list() #先打印清單
print('\n'*3)
print("歡迎下次光臨")
import os
os._exit(0)
cmd_dict = {'a':add,'e':edit,'d':delete,'p':payment,'s':show_goods}
#定義函數,顯示命令提示
def show_command():
cmd = input("請輸入操作指令:\n"+" 添加(a) 修改(e) 刪除(d) 結算(p) 超市商品(s)\n")
if cmd not in cmd_dict:
print("輸入有誤,請重新輸入")
else:
cmd_dict[cmd]()
init_repository()
show_goods()
while True:
show_list()
show_command()
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的python超市管理系统_控制台超市系统(Python)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mongodb同时更新一条记录_Mong
- 下一篇: postman cookie设置_是时候