码迷,mamicode.com
首页 > 其他好文 > 详细

列表--购物

时间:2019-12-07 10:48:03      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:oppo   http   alt   lease   取消   ota   ima   else   用户   

程序功能:

1、启动程序后,让用户输入工资,然后打印商品列表

2、允许用户根据商品编号购买商品

3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒

4、可随时退出,退出时,打印已购买商品和余额

 

your_salary = int(input("Please your salary:"))
goods = [[1, "IPhone", 5900], [2, "OPPO", 3700], [3, "MI", 1000], [4, "HP COMPUTER", 2000], [5, "MAC", 10000]]
favorite_goods = []

print("Number\tGoods\tPrice")
for i in goods:
    print("%s\t%8s\t%5s" % (i[0], i[1], i[2]))

print("输入非数字,结束购物并跳转付款菜单")
while True:
    goods_number = input("Please choose your favorite goods:")
    if goods_number.isdigit():  #如果goods_number是数字,强转成int型
        goods_number = int(goods_number)
    else:
        break
    if goods_number > len(goods):  #输入的编号不存在,跳转到循环开头
        print("Goods is not exist.")
        continue

    favorite_goods.append(goods[goods_number-1])  #选择的商品加入到购物车列表中
    print(goods[goods_number-1][1] + " 已加入到购物车中")
    total_goods = 0
    for i in favorite_goods:  #统计购物车价格
        total_goods += i[2]
    if total_goods > your_salary:
        print("购买的商品总额为 %s,已经超过了您的工资 %s" % (total_goods, your_salary))
        print("已为您清掉商品%s" % (favorite_goods.pop()[1]))

if favorite_goods == []:  #favorite_goods列表为空,退出程序
    print("购物车为空,程序退出")
    quit(0)
print("您购物车商品如下:")
print("Number\tGoods\tPrice")
for i in favorite_goods:  #打印购物车
    print("%s\t%8s\t%5s" % (i[0], i[1], i[2]))

print(-*8)
buy_goods = input("Do you sure to buy(b/B) ?")
if buy_goods == b or buy_goods == B:  #购买商品,并显示余额
    print("所购商品总价格: %s" %(total_goods))
    print("您的工资剩余:%s" % (your_salary-total_goods))
else:
    print("取消购买购物车的商品")
    print("购物车总价格: %s" %(total_goods))
    print("工资剩余:%s" % (your_salary))

 

 技术图片

 

 

列表--购物

标签:oppo   http   alt   lease   取消   ota   ima   else   用户   

原文地址:https://www.cnblogs.com/leejay/p/11997250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!