This article mainly introduces the implementation of shopping cart shopping applet in Python in detail, which has certain reference value. Interested friends can refer to it
Summary
Logically speaking, the first small program we start with should be Hello World. Because it's relatively simple, I won't do too many demonstrations here.
The following is a small program I wrote. Mainly used to practice the basic syntax of Python and get started.
Main Implementation functions
Require users to enter their expected consumption amount.
Display existing product information and require the user to select
After the user selects the corresponding product number (note whether the expected consumption limit is exceeded, etc.), save it to the shopping cart
Shopping cart information and remaining balance will be displayed after the user exits
Code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @author: 烽火 @license: Apache Licence @file: shopping.py @time: 6/16/17 10:05 AM """ goods = [ ("IPhone", 5800), ("Watch", 2000), ("MacBook", 12000)] goods_cart = [] mybudget = input("请输入您的预算:") # 不考虑是小数的情况 while not mybudget.isdigit(): mybudget = input("输入有误,请重新输入您的预算:") mybudget = int(mybudget); while True: print("商品列表".center(50, "-")) print("编号".center(8, " "), "名称".ljust(30, " "), "价格".ljust(10, " ")) for i in enumerate(goods): print(str(i[0]).center(10, " "), str(i[1][0]).ljust(31, " "), str(i[1][1]).ljust(10, " ")) user_choose = input("请输入您的选择:") 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("预算还有%d" % mybudget) else: print("预算不够啦~") else: print("不存在该商品~") elif user_choose == 'q': break else: print("您的输入有误~") print("预算还剩%d了" %(mybudget)) print("购物车商品信息".center(50, "-")) for i in enumerate(goods_cart): print(str(i[0]).center(10, " "), str(i[1][0]).ljust(31, " "), str(i[1][1]).ljust(10, " "))
operation result
Related recommendations:
Python3 implements shopping cart function
##
The above is the detailed content of Python implements shopping cart shopping applet. For more information, please follow other related articles on the PHP Chinese website!