python写简单购物车_使用Python实现简单购物车

#!/usr/bin/env python# -*- coding: utf-8 -*-shopping_list = [('Iphone',5800),('Bike',800),('Book',45),('Coffee',35),('Solo 2 Beats',1590),('MX4',1999),]#定义一个商品列表budget = int(raw_input("please input your budget:").strip())#输入预算buy_list = []#定义购物车列表while True:    for i in shopping_list:        print shopping_list.index(i),i#循环打印出商品的index和商品名称    choice = int(raw_input("please input your choice:").strip())#输入选择的商品,strip()表示忽略空格    item_price = shopping_list[choice][1]#输出选择商品的价格,这里把(‘xxx’,xxxx)当成两个元素,单独取第二个输出为价格    print item_price    #判断预算是否大于商品价格,如果大于就减去当前商品,打印输出已经购买的商品,和剩余的预算;否则提示重新输入    if budget >= item_price:        budget -= item_price        buy_list.append(shopping_list[choice])        print "Added \033[1;33m %s \033[0m into shopping list." % shopping_list[choice][0]        print "You just  only have \033[1;32m %s \033[0m. \n" % budget    else:        print "Sorry, you can not afford to buy %s,try another!" %shopping_list[choice][0]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值