python购物车程序

 

 很基础的一个例子,主要是为了练习python的基础语法,要求如上,实现代码如下:

# coding=gbk
# _author:Administrator
# date: 2021/03/31

GoodsDic = {"iphone6s": 5800, "bicycle": 1500, "python book": 80, "coffee": 30, "mac book": 10000}
GoodsDic1 = {'1': "iphone6s", '2': "bicycle", '3': "python book", '4': "coffee", '5': "mac book"}
# print(GoodsDic1['1'])
salary = input("您的工资为?\n")
goodList = []
isExit = 1
while isExit:
    goodNum = input("请输入你要购买的商品序号:\n1.iphone6s,2.bicycle,3.python book,4.coffee,5.mc book,6.exit\n")
    for good in GoodsDic:
        if goodNum == '6':
            isExit = 0
            break
        if good == GoodsDic1[goodNum]:
            if int(salary) >= GoodsDic[good]:
                goodList.append(good)
                salary = int(salary) - GoodsDic[good]
                msg = "已购买%s,余额为:%s\n" % (good, salary)
                print(msg)
                break
            else:
                print("您的资金不足,请选择别的商品!")
                break

print("您购买了以下商品:\n")
for g in goodList:
    print(g+"\t")
msg = "余额为:%s,欢迎光临\n" % salary
print(msg)

输出结果:

您的工资为?
1000
请输入你要购买的商品序号:
1.iphone6s,2.bicycle,3.python book,4.coffee,5.mc book,6.exit
2
您的资金不足,请选择别的商品!
请输入你要购买的商品序号:
1.iphone6s,2.bicycle,3.python book,4.coffee,5.mc book,6.exit
3
已购买python book,余额为:920

请输入你要购买的商品序号:
1.iphone6s,2.bicycle,3.python book,4.coffee,5.mc book,6.exit
6
您购买了以下商品:

python book    
余额为:920,欢迎光临

 

posted @ 2021-03-31 21:10  WellMandala  阅读(140)  评论(0编辑  收藏  举报