日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python实现一个商品管理_python编写商品管理

發布時間:2024/4/13 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python实现一个商品管理_python编写商品管理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

# 1、實現一個商品管理的程序。

# #輸出1,添加商品 2、刪除商品 3、查看商品

# 添加商品:

# 商品的名稱:xxx 商品如果已經存在的話,提示商品商品已經存在

# 商品的價格:xxxx 數量只能為大于0的整數

# 商品的數量:xxx,數量只能為大于0的整數

# 2、刪除商品:

# 輸入商品名稱:

# iphone 如果輸入的商品名稱不存在,要提示不存在

# 3、查看商品信息:

# 輸入商品名稱:

# iphone:

# 價格:xxx

# 數量是:xxx

# all:

# print出所有的商品信息

import json

def add_product():

product = input('請輸入商品名稱:').strip()

count = input('請輸入商品數量:').strip()

price = input('請輸入商品價格:').strip()

f = open('product.json', 'a+', encoding='utf-8')

f.seek(0)

products = json.load(f)

if product == '':

print('商品名稱不能為空')

elif product in products:

print('商品已存在')

elif not count.isdigit():

print('商品數量必須為正整數')

elif not price.isdigit():

print('商品價格必須為正整數')

else:

products[product] = {}

products[product]['count'] = int(count)

products[product]['price'] = int(price)

f.seek(0)

f.truncate()

json.dump(products, f, indent=4, ensure_ascii=False)

f.close()

def show_product(product):

f = open('product.json', encoding='utf-8')

products = json.load(f)

f.close()

if (product=='all'):

return products

elif not (product in products):

print('商品不存在')

else:

#print(products[product])

return product+':\n 數量:'+str(products[product]['count'])+'\n 價格:'+str(products[product]['price'])

def del_product(product):

f = open('product.json', 'a+', encoding='utf-8')

f.seek(0)

products = json.load(f)

if not (product in products):

print('商品不存在')

else:

del products[product]

f.seek(0)

f.truncate()

json.dump(products, f, indent=4, ensure_ascii=False)

f.close()

print("輸出1、添加商品 2、刪除商品 3、查看所有商品")

choice=input()

if choice=="1":

add_product()

elif choice=="2":

product=input('請輸入要刪除的商品名稱:')

del_product(product)

elif choice=="3":

product=input('請輸入要查詢的商品名稱:')

print(show_product(product))

else:

print('輸入有誤')

總結

以上是生活随笔為你收集整理的python实现一个商品管理_python编写商品管理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。