#!/usr/bin/env python#-*- coding:utf-8 -*-#简单的购物车 # Knowledge Points: #1: Graceful Enumerate function, returns a Generator object: (Index, Item) tuple #2:print (' product list : ') can center print, really fun, haha goods = [ ("IPhone", 5800), ("Watch", "$"), ("Mac", 12000)]goods _cart = []mybudget = raw_input ("Please enter your budget:") While not mybudget.isdigit (): mybudget = input ("Your budget input is wrong, please re-enter.") Mybudget = int (mybudget) while true: print "Product List". Center (50, '-') print "number". Center (8, ' '), "name". Ljust (+, ' '), "Price". Ljust (' ') for i in enumerate (goods): #enumerate函数会返回下标和取值 print str (I[0]). Center (6, ' '), str (i[1][0]). Ljust (28, ' '), str (i[1][1]). Ljust (' ') user_choose = raw_input ("Please enter your choice:") if user_choose.isdigit (): user_choose = int (User_choose) if user_choose >=0 and user_choose <len (goods): #先判断商品是否存在 if mybudget - goods[ user_choose][1] > 0: #在判断预算是否大于0 mybudget -= goods[user_choose][1] goods_cart.append (Goods[user_choose]) #商品加入购物车 print "Budget is%d." % (MyBudget) else: & nbsp; print "budget is not enough." else: print "The product does not exist." elif user_choose == "Q": breakprint "Budget%d" % (mybudget) print "Shopping cart product Information". Center ("-") for i in Enumerate (Goods_cart): print str (I[0]). Center (6, ' '), str (I[1][0]) . Ljust (28, ' '), str (I[1][1]). Ljust (12, ' ')
650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/20/39346f909b37f50c81a207c503a77a98.png-wh_500x0-wm_3 -wmp_4-s_1682275697.png "title=" shopping cart. PNG "alt=" 39346f909b37f50c81a207c503a77a98.png-wh_ "/>
This article from "Kong Love to Eat fish" blog, declined reprint!
"Python" simple shopping cart