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('\n此商品不存在,请重新输入...') continue break product = product_list[i] price = int(product[1]) # 判断余额是否充足,够就直接扣款,不够提醒 if price <= balance: balance -= price shopping_list.append(product_list[i]) print('\n您已成功购买 %s ,当前余额为 %d 元' %(product, balance)) else: print('\n购买失败,您的余额不足...') while True: a = input('\n是否继续购买?(Y/N):') if a != 'n' and a != 'N' and a != 'y' and a != 'Y': print('\n输入格式有误,请重试...') continue break if a == 'Y' or a == 'y': continue else: break 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()
The above is the detailed content of Python's introduction to mall shopping applets. For more information, please follow other related articles on the PHP Chinese website!