# -*- coding: utf-8 -*-
'''
1.启动程序后让用户输入消费额,然后打印商品列表'
2.允许用户根据商品编码购买商品
3.用户选择商品后,检测余额是否够,够则直接扣款,不够就提醒
4.可以随时退出,退出时,打印已购买的商品和余额
'''
# 定义商品列表
global commodList
commodList = {"笔记本": [["华为", 1000, 11], ["戴尔", 2000, 12], ["外星人", 3000, 13], ["联想", 500, 14]], /
"手机": [["华为", 999, 21], ["小米", 888, 22], ["苹果", 777, 23], ["oppo", 666, 24]], /
"冰箱": [["格力", 2222, 31], ["海尔", 1111, 32], ["海信", 3333, 33], ["奥克斯", 4444, 34]], /
"电视": [["长虹", 5555, 41], ["海信", 6666, 42], ["小米", 8888, 43], ["夏新", 3333, 44]]}
# 得到用户余额
def getUserMoney():
money = int(input("请输入您的余额:/n"))
return money
# 显示物品菜单
def showCommodity():
print("欢迎来到新时代购物广场:您可以任意限购以下商品:", end="/n")
for commodType in commodList.keys():
print(commodType + ":", end="/n")
commodName = commodList[commodType]
# print(commodName) //[['华为', 1000], ['戴尔', 2000], ['外星人', 3000], ['联想', 500]]
for commodName1 in commodName:
print("品牌:{_commodName}<---->价格:{_commodPrice}<---->商品编码:{_commodNumber}" /
.format(_commodName=commodName1[0], _commodPrice=commodName1[1], /
_commodNumber=commodName1[2]))
# 根据商品编码得到商品价格及商品名称
def getCommdityPrice(commodNum):
# 判断商品是否存在
if commodNum != 99999:
flag = False
# 根据物品编码得到物品
for commodName in commodList.keys():
for commodNum1 in commodList[commodName]:
if commodNum1[2] == commodNum:
flag = True
return commodNum1[1], commodNum1[0]
if flag == False:
print("{_commodNum}商品编码不存在".format(_commodNum=commodNum))
else:
print("结束购物!!")
# 增加购物车
def shoppingGet():
# 定义用户余额
UserMoney1 = getUserMoney()
# 定义用户购买的物品列表
UserCommList = []
showCommodity()
while True:
# 购买商品列表
shoppNum = int(input("请选择你想购买的物品编码,如果输入 99999 则表示本次选购已完成!:/n"))
# 商品名称和商品价格
try:
commodNum = getCommdityPrice(shoppNum)[1]
money1 = int(getCommdityPrice(shoppNum)[0])
except Exception as e:
print("")
if shoppNum == 99999:
print(UserCommList, UserMoney1)
UserCommListName = []
for commodNo in UserCommList:
UserCommListName.append(getCommdityPrice(commodNo)[1])
print(
"您现在购买的商品:{_UserCommList},余额为:{_UserMoney1}".format(_UserCommList=UserCommListName,
_UserMoney1=UserMoney1))
print()
break
# 判断money够用
elif money1 <= UserMoney1:
# 如果足够则把商品加到购物车列表中
UserCommList.append(shoppNum)
# 增加后,扣款
UserMoney1 = UserMoney1 - money1
print(
"您现在购买的商品:{_UserCommList},余额为:{_UserMoney1}".format(_UserCommList=UserCommList, _UserMoney1=UserMoney1))
# 判断money不够用
else:
# 如果余额不足以购买则输出
print("您的余额为{_Money},不足以购买<{_commodNum}>".format(_Money=UserMoney1, _commodNum=commodNum))
if __name__ == '__main__':
shoppingGet()
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/python/280168.html