"Python" simple shopping cart

Source: Internet
Author: User

#!/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.