欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)倉(cāng)庫(kù)管理系統(tǒng)

 更新時(shí)間:2022年05月30日 14:31:19   作者:運(yùn)維@小兵  
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)倉(cāng)庫(kù)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Python實(shí)現(xiàn)倉(cāng)庫(kù)管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

注意:在Linux環(huán)境運(yùn)行

代碼

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName ?:store_system.py
# @Time ? ? ?:2020/3/3 23:10
# @Author ? ?:anqixiang
# @Function ?:模擬倉(cāng)庫(kù)管理系統(tǒng)
'''
1.商品清單保存在/opt/shop_info.txt文件中
2.可以查看、增加、刪除商品和修改商品價(jià)格
3.在任何位置輸入b返回上級(jí)菜單,輸入q退出
'''
import os
from subprocess import run

#輸出顏色
def cecho(num,content):
? ? print('\033[%sm%s\033[0m' %(num, content))

#選b返回上一層,選q退出
def choice_action(action):
? ? while action != "b":
? ? ? ? if action == "q":
? ? ? ? ? ? exit(0)
? ? ? ? else:
? ? ? ? ? ? break
? ? return action

#展示商品
def view_shop(file_name):
? ? commodity = [] ? ? ? ? ? ? ?#所有商品保存到該列表
? ? if not os.path.isfile(file_name):
? ? ? ? os.mknod(file_name)
? ? else:
? ? ? ? with open(file_name, 'r') as file:
? ? ? ? ? ? for each in file:
? ? ? ? ? ? ? ? commodity.append(each.splitlines())
? ? if len(commodity) == 0:
? ? ? ? cecho(35, "貨倉(cāng)空空如也,請(qǐng)速速添加商品!")
? ? ? ? #打印商品信息
? ? else:
? ? ? ? print('%-10s%-8s%-12s' % ('序號(hào)', '名字', '價(jià)格'))
? ? ? ? for index, value in enumerate(commodity):
? ? ? ? ? ? alist = value[0].split(":") ? ? ? ? #把字符串轉(zhuǎn)成列表,以“:”分割
? ? ? ? ? ? print('%-12s%-10s%-8s' % (index + 1, alist[0], alist[1]))
? ? return commodity

#增加商品,每增加一個(gè)就保存到文件
def add_shop(file_name):
? ? while True:
? ? ? ? add_dict = {}
? ? ? ? shop_name = input(">>>輸入商品名:").strip()
? ? ? ? if choice_action(shop_name) == "b":
? ? ? ? ? ? break
? ? ? ? shop_price = input(">>>輸入商品價(jià)格(元):").strip()
? ? ? ? if choice_action(shop_price) == "b":
? ? ? ? ? ? break
? ? ? ? elif shop_price.isdigit():
? ? ? ? ? ? add_dict[shop_name] = shop_price ? ? ? ?#商品名作key,價(jià)格作值,存入字典
? ? ? ? ? ? for i in add_dict:
? ? ? ? ? ? ? ? with open(file_name, 'a+')as file:
? ? ? ? ? ? ? ? ? ? file.write('%s:%s\n' % (i, add_dict[i]))
? ? ? ? ? ? ? ? ? ? print("\033[92m%s存入成功\033[0m" % shop_name)
? ? ? ? ? ? ? ? view_shop(file_name)
? ? ? ? else:
? ? ? ? ? ? cecho(31, "Invalid Option")

#刪除商品
def del_shop(file_name):
? ? menu_info = "商品清單"
? ? print(menu_info.center(26,'-'))
? ? commodity = view_shop(file_name)
? ? while True:
? ? ? ? del_num = input(">>>商品序號(hào):").strip()
? ? ? ? if choice_action(del_num) == "b":
? ? ? ? ? ? break
? ? ? ? elif del_num.isdigit():
? ? ? ? ? ? del_num = int(del_num)
? ? ? ? ? ? rc = run("sed -i '/%s/d' %s" % (commodity[del_num-1][0], file_name), shell=True)
? ? ? ? ? ? if not rc.returncode:
? ? ? ? ? ? ? ? cecho(92, "刪除成功")
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? cecho(31,"刪除失敗")
? ? ? ? ? ? view_shop(file_name)
? ? ? ? else:
? ? ? ? ? ? cecho(31, "Invalid Option")

#修改商品價(jià)格
def update_price(file_name):
? ? menu_info = "商品清單"
? ? print(menu_info.center(26,'-'))
? ? commodity = view_shop(file_name)
? ? while True:
? ? ? ? update_num = input(">>>商品序號(hào):").strip()
? ? ? ? if choice_action(update_num) == "b":
? ? ? ? ? ? break
? ? ? ? elif update_num.isdigit():
? ? ? ? ? ? update_num = int(update_num)
? ? ? ? else:
? ? ? ? ? ? cecho(31, "Invalid Option")

? ? ? ? new_price = input(">>>新的價(jià)格(元):").strip()
? ? ? ? if choice_action(new_price) == "b":
? ? ? ? ? ? break
? ? ? ? elif new_price.isdigit():
? ? ? ? ? ? new_price = int(new_price)
? ? ? ? ? ? alist = commodity[update_num-1][0].split(':') ? #將商品名和價(jià)格轉(zhuǎn)成一個(gè)列表,如['coffee', '30']
? ? ? ? ? ? alist[1] = new_price ? ? ? ?#修改價(jià)格
? ? ? ? ? ? rc = run("sed -i '/%s/c %s:%s' %s" % (alist[0], alist[0], alist[1], file_name), shell=True)
? ? ? ? ? ? if not rc.returncode:
? ? ? ? ? ? ? ? cecho(92, "修改成功")
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? cecho(31,"修改失敗")
? ? ? ? ? ? view_shop(file_name)
? ? ? ? else:
? ? ? ? ? ? cecho(31, "Invalid Option")

#主程序
def show_menu():
? ? cmds = {'0': view_shop, '1': add_shop, '2': del_shop, '3': update_price}
? ? prompt = '''(0)查看商品信息
(1)增加商品
(2)刪除商品
(3)修改商品價(jià)格
(b)返回上級(jí)菜單
(q)退出
輸入(0/1/2/3/b/q):'''
? ? fname='/opt/shop_info.txt' ? ? ?#保存商品信息
? ? while True:
? ? ? ? choice = input(prompt).strip()
? ? ? ? if choice not in '0123bq':
? ? ? ? ? ? cecho(31, "Invalid Option")
? ? ? ? elif choice_action(choice) == "b":
? ? ? ? ? ? cecho(31, "已經(jīng)是第一級(jí)菜單")
? ? ? ? else:
? ? ? ? ? ? cmds[choice](fname)

if __name__ == "__main__":
? ? try:
? ? ? ? show_menu()
? ? except KeyboardInterrupt as e:
? ? ? ? print()
? ? ? ? cecho(31, "非正常退出,請(qǐng)下次輸入字母q進(jìn)行退出!")

效果圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論