Python implementation shopping cart shopping applet

Source: Internet
Author: User
This article is mainly for everyone to introduce the python to achieve shopping cart shopping small program, with a certain reference value, interested in small partners can refer to

Profile

Supposedly, the first small program we get started with is Hello World. Because it is relatively simple, I will not do too much demonstration.
Here is a small program that I wrote. Primarily used to practice Python's basic syntax, as well as getting started.

Main Implementation Functions

    • Ask the user to enter their expected spending amount.

    • Display existing product information and ask the user to select

    • After the user selects the corresponding product label (pay attention to determine whether to exceed the expected amount of consumption, etc.), save to the shopping cart

    • Display of cart information and remaining amount after user exits

Code:

#!/usr/bin/env python#-*-coding:utf-8-*-"" "@author: Beacon @license:apache licence@file:shopping.py@time:6/16/17 10:05 A M "" "Goods = [(" IPhone ", 5800), (" Watch "," $ "), (" MacBook ", 12000)]goods_cart = []mybudget = input (" Please enter your budget: ") # no consideration is a decimal When not Mybudget.isdigit (): MyBudget = input ("Wrong input, reenter your budget:") MyBudget = Int (mybudget), while True:print ("Commodity list". C Enter ("-")) Print ("number"). Center (8, ""), "name". Ljust (30, ""), "Price". Ljust (Ten, "")) for I in Enumerate (goods): print  (Str (i[0]). Center (Ten, ""), str (i[1][0]). Ljust (""), str (i[1][1]). Ljust = 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:goods_cart.append (Goods[user_choose]) mybudget-= Goods[user_ CHOOSE][1] Print ("Budget also%d"% MyBudget) else:print ("Budget Not Enough ~") else:print ("There is no such product ~") Elif user_c Hoose = = ' Q ': Break ELse:print ("Your input is wrong ~") print ("The budget has%d"% (MyBudget)) print ("Shopping cart product Information". Center ("-") ") for I in Enumerate (goods_cart): pri NT (str (I[0)). Center (Ten, ""), str (i[1][0]). Ljust (""), str (i[1][1]). Ljust (10, ""))

Run results

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.