使用Python实现商品的订购统计,输入和输出由控制台实现。
代码如下:
# 商品字典
seller_dict = {}
# 用户字典
user_dict = {}
# 添加商品信息,返回值为商品信息字典
def seller_dict_info():
i = 1
while (True):
seller_n = input(f"【输入0结束输入】请输入第{i}个商品名称:")
if seller_n == '0':
break
seller_p = float(input(f"请输入第{i}个商品价格:"))
seller_dict[seller_n] = seller_p
i = i + 1
return seller_dict
def user_seller_info():
res = seller_dict_info()
print("商品列表为:", res)
# 总字典
user_seller_dict = {}
# key值列表,即商品名称列表
k_list = []
# value值列表,即商品价格列表
v_list = []
for k in res.keys():
k_list.append(k)
for v in res.values():
v_list.append(v)
j = 1
print("key", k_list)
print("value", v_list)
while (True):
user_c_list = []
sum_price = 0
user_n = input(f"【输入0结束输入】请输入第{j}个用户名称:")
j = j + 1
if user_n == '0':
break
for seller_name in k_list:
user_c = int(input(f"输入此用户订购【{seller_name}】商品的份数:"))
user_c_list.append(user_c)
print("用户份数列表", user_c_list)
for i in range(len(user_c_list)):
price = float(user_c_list[i]) * v_list[i]
sum_price = sum_price + price
sum_price = round(sum_price, 3)
print("此用户最终价格为:", sum_price)
user_seller_dict[user_n] = sum_price
print(user_seller_dict)
# 算总价
# 总价格
total_sum_price = 0
total_sum_count = 0
for p in user_seller_dict.values():
total_sum_price = total_sum_price + p
total_sum_count = total_sum_count + 1
print("*" * 30)
for k, v in user_seller_dict.items():
print(f"{k}:{v}元")
print(f"总价格:{round(total_sum_price)}元")
print(f"共{total_sum_count}个用户信息")
if __name__ == '__main__':
user_seller_info()
运行结果: