Home > Backend Development > Python Tutorial > Python implements shopping cart shopping applet

Python implements shopping cart shopping applet

不言
Release: 2018-04-18 11:08:16
Original
2212 people have browsed it

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 == &#39;q&#39;:
    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, " "))
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template