Python List 练习实践(二)

地址:http://blog.csdn.net/u011463646/article/details/77104026

List 存取数字

本文尝试用python实现简单的购物车程序。。。
基本要求:
用户输入工资,然后打印购物菜单
用户可以不断的购买商品,直到余额不够为止
退出时打印用户已购买的商品和剩余金额。。。
代码:

Product_List = [("Apple", 5800), ("Bike", 800), ("Python", 80), ("Mac", 12000), ("NoteBook", 10)]  # 使用了元组
Current_List = []
salary = input("Your salary: ")
if salary.isdigit():
    salary = int(salary)
    while True:
        print("---index      product     price---")
        for index, item in enumerate(Product_List):
            print("   %d          %s         %d$" % (index, item[0], item[1]))
        choose_item = input("Choose index: ")
        if choose_item.isdigit():
            choose_item = int(choose_item)
            if (choose_item < len(Product_List)) and (choose_item >= 0):
                Current_pro = Product_List[choose_item]
                if Current_pro[1] < salary:
                    salary -= Current_pro[1]
                    Current_List.append(Current_pro)
                    print(" Your Current List:%s   Balance is: \033[31;1m %d \033[0m" % (Current_List, salary))
                else:
                    print("\033[43;1m Your money is not enough!!!  %d \033[0m"%salary)
            else:
                print("\033[45;1m Invalid index! Continue... \033[0m")
                continue

        elif choose_item == "q":
            print("-------Your List-------")
            for item in Current_List:
                print(item[0],"--->>",item[1])
            exit("Final balance is:%s"%salary)

        else:
            print("\033[41;1m Wrong Command! Continue...\033[0m")
            continue
else:
    print("Invalid  input !")

效果

这里写图片描述
这里写图片描述

参考

【1】python实现购物车程序 - 快递小可的博客 - CSDN博客
http://blog.csdn.net/sxingming/article/details/52334488

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何以问天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值