How to use Python to implement shopping program ideas and implementation code

黄舟
Release: 2017-07-24 15:26:08
Original
2763 people have browsed it

This article shares with you the idea requirements and related codes of the shopping applet implemented in Python. It is very simple and practical. Friends in need can refer to the following

Requirements:

After starting the program, let the user enter their salary, and then print out a product list with serial numbers
The user enters the product serial number to purchase the corresponding product, or enters 'q' to exit the purchase interface
After selecting the product, check whether the balance is sufficient. If it is enough, the money will be deducted directly. If it is not enough, it will prompt that the balance is insufficient.
After the user purchases an item, or enters 'q' to exit the purchase interface, it will prompt: Do you want to continue purchasing? (Y/N), to achieve multiple purchases
If the user purchases goods, print out the purchased goods list, total amount, and balance; if the user does not buy any goods, print: Transaction ended, shopping failed
Readme:

Run the program, enter your salary, and select the product to purchase according to the serial number in the product list. You can choose to purchase multiple times or not to purchase

Flow chart:

Code:


# 简单的购物小程序

product_list = [
  ['surface pro 4', 7800],
  ['dell xps 15', 12000],
  ['macbook', 12000],
  ['小米6', 2499],
  ['iphone7', 4600],
  ['坚果Pro', 1499]
]
shopping_list = []


# 判断输入的薪水格式是否正确
while True:
  salary = input('\n请输入您的薪水:')
  if not salary.isdigit():          # 薪水不是数字,结束循环
    print('\n输入格式有误!请重新输入...')
    continue
  break


balance = salary = int(salary)

print('\n-----------欢迎购买------------\n')

# 生成带序号的商品列表
for index, item in enumerate(product_list):
  print(index, item)


# 判断输入的序号是否符合要求
while True:

  while True:
    i = input('\n输入您要购买的商品序号,或输入 q 取消购买:')
    if i == 'q':                 # 输入 q 退出购买界面
      while True:
        a = input('\n是否继续购买?(Y/N):')
        if a != 'n' and a != 'N' and a != 'y' and a != 'Y':
          print('\n输入格式有误,请重试...')
          continue
        elif a == 'y' or a == 'Y':         # 继续购买
          break
        else:                    # 购买完毕
          if balance == salary:       # 没有买任何东西
            print('\n交易结束,购买失败...')
            exit()
          else:               # 结算  
            print('\n您已成功购买以下商品:\n')
            for item in shopping_list:
              print(item)
            print('\n共消费金额 %d 元,余额 %d 元' % (salary - balance, balance))
            exit()
      continue

    if not i.isdigit():             # 序号不是数字,结束循环
      print('\n输入格式有误!请重新输入...')
      continue

    i = int(i)

    if i < 0 or i >= len(product_list):  # 序号范围不正确,结束循环
      print(&#39;\n此商品不存在,请重新输入...&#39;)
      continue
    break

  product = product_list[i]
  price = int(product[1])

  # 判断余额是否充足,够就直接扣款,不够提醒
  if price <= balance:
    balance -= price
    shopping_list.append(product_list[i])
    print(&#39;\n您已成功购买 %s ,当前余额为 %d 元&#39; %(product, balance))
  else:
    print(&#39;\n购买失败,您的余额不足...&#39;)

  while True:
    a = input(&#39;\n是否继续购买?(Y/N):&#39;)
    if a != &#39;n&#39; and a != &#39;N&#39; and a != &#39;y&#39; and a != &#39;Y&#39;:
      print(&#39;\n输入格式有误,请重试...&#39;)
      continue
    break

  if a == &#39;Y&#39; or a == &#39;y&#39;:
    continue
  else:
    break

if balance == salary:
  print(&#39;\n交易结束,购买失败...&#39;)
  exit()
else:
  print(&#39;\n您已成功购买以下商品:\n&#39;)
  for item in shopping_list:
    print(item)
  print(&#39;\n共消费金额 %d 元,余额 %d 元&#39; %(salary-balance, balance))
  exit()
Copy after login

The above is the detailed content of How to use Python to implement shopping program ideas and implementation code. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!