有商品编号列表和商品数量如下:
product=["010105","010106","010107","010108","010109","010110","010111]
num=[12,13,11,9,6,17,8]
(1)用商品编号列表和商品数量列表生成字典。置(以商品编号为键,商品数量为值)
(2)编写函数dec0实现功能:提供个商品编号 ,判断该商品是否在字典中存在,如果字典中有该商品,则对修改该商品的数量,在其原有数量的基础上减少m个;如果没有该商品,则输出提示”该商品不存在!"。
你现在有字典了
创建字典后以商品编号作为字典的键对字典查找,如果查找到了就将其值更新即可
有帮助望采纳
具体代码如下:
product = ["010105", "010106", "010107",
"010108", "010109", "010110", "010111"]
num = [12, 13, 11, 9, 6, 17, 8]
dict1 = {k: v for k, v in zip(product, num)}
print(dict1)
def dec0():
scan_number = input('请输入商品ID:\n')
if scan_number not in dict1.keys():
print('该商品不存在!')
else:
reduce_num = int(input('请输入要减少的数量:\n'))
dict1[scan_number] -= reduce_num
# print(dict1[scan_number])
if __name__ == '__main__':
while True:
dec0()
print(dict1)
你题目的解答代码如下:(如有帮助,望采纳!谢谢! 点击我这个回答右上方的【采纳】按钮)
product=["010105","010106","010107","010108","010109","010110","010111"]
num=[12,13,11,9,6,17,8]
dic = dict(zip(product,num)) #用商品编号列表和商品数量列表生成字典
print(dic)
def dec0(sid):
if sid in dic:
m = int(input(f"请输入编号{sid}商品减少的数量:"))
dic[sid] -= m
print(f'商品编号{sid}修改后的数量为:{dic[sid]}')
else:
print('该商品不存在!')
sid = input("请输入商品编号:")
dec0(sid)
product = ["010105", "010106", "010107", "010108", "010109", "010110", "010111"]
num = [12, 13, 11, 9, 6, 17, 8]
data = dict(zip(product, num)) # 1 将数据合并为字典
def dec0(product_id, m):
"""
2 根据 product_id查找产品,并减去m库存
:param product_id:
:param m:
:return:
"""
global data
if product_id in data: # 如果编程存在
data[product_id] -= m # 就减少库存
else: # 否则
print("该商品不存在!") # 提示不存在
dec0("xxxxx", 2) # 调用函数,不存在的变编号输出 "该商品不存在!"
dec0("010105", 2) # 调用函数,减少库存
print(data) # 打印减少库存后的数据