FILENAME='products.json' #常量 import json def get_file_content(): with open(FILENAME,encoding='utf-8') as f : content = f.read() if len(content)>0: res = json.loads(content) else: res = {} return res def write_file_content(dic): with open(FILENAME,'w',encoding='utf-8') as fw: json.dump(dic,fw,indent=4,ensure_ascii=False) def check_digit(st:str): if st.isdigit(): st = int(st) if st>0: return st else: return 0 else: return 0 def add_product(): product_name = input('請輸入商品名稱:').strip() count = input('請輸入商品數量:').strip() price = input('請輸入商品價格:').strip() all_products = get_file_content() if check_digit(count) == 0: print('數量輸入不合法') elif check_digit(price) == 0: print('價格輸入不合法') elif product_name in all_products: print('商品已經存在') else: all_products[product_name] = {"count":int(count), "price":int(price)} write_file_content(all_products) print('添加成功!') def show_product(): product_name = input('請輸入要查詢的商品名稱:').strip() all_products = get_file_content() if product_name=='all': print(all_products) elif product_name not in all_products: print('商品不存在') else: print(all_products.get(product_name)) def del_product(): product_name = input('請輸入要刪除的商品名稱:').strip() all_products = get_file_content() if product_name in all_products: all_products.pop(product_name) print('刪除成功') write_file_content(all_products) else: print('商品不存在') choice = input('請輸入你的選擇:\n 一、添加商品 二、刪除商品 三、查看商品信息') if choice=="1": add_product() elif choice=="2": del_product() elif choice=="3": show_product() else: print('輸入錯誤,請從新輸入!')