Python实践:购物车
2018-09-17 19:48
330 查看
版权声明:个人博客网站:https://cunyu1943.github.io/, 欢迎访问留言交流! https://blog.csdn.net/github_39655029/article/details/82747768
购物车程序
- 要求如下图
- 代码
# --*--coding:utf-8--*-- # Author: 村雨 import pprint productList = [('Iphone 8', 10000), ('GTX2080', 8000), ('Z7KP7-GT', 6000), ('Mac pro', 15000), ('Honor 10', 2800), ('Iphone XR', 12000), ('Mi 8', 2999) ] shoppingList = [] print('输入你的工资:') salary = input() if not salary.isdigit(): print('请输入整数') else: salary = int(salary) while True: for index, item in enumerate(productList): print(index + 1, item) print('输入你要买的商品的序号:') userWant = input() if userWant.isdigit(): userWant = int(userWant) if userWant <= len(productList) and userWant > 0: print('你要购买的是:', productList[userWant - 1][0]) if salary >= productList[userWant - 1][1]: shoppingList.append(productList[userWant - 1][0]) salary -= productList[userWant - 1][1] print('你已经购买了' + productList[userWant - 1][0] + ', 你的余额为 ' + str(salary)) else: print('对不起,你的余额不足!请努力工作吧!') print('你当前所购买的商品为:') for brought in shoppingList: pprint.pprint(brought) print('你当前余额为:', salary) exit() else: print('你输入的商品序号有错,请重新输入') elif userWant == 'q': print('-----------Shopping List----------') for brought in shoppingList: pprint.pprint(brought) print('你的余额为 ', salary) exit() else: print('Invalid input!!!')[/code]
- 结果
相关文章推荐
- python简单商城购物车实例代码
- 比PCA降维更高级——(R/Python)t-SNE聚类算法实践指南
- python入门实践1-11课视频分享
- python爬虫实践(一):准备工作
- 实践多种搜索算法求解八数码问题python实现
- 基础入门_Python-模块和包.运维开发中MySQLdb CRUD操作的最佳实践?
- Python-决策树ID3实践
- 详解Python 2.6 升级至 Python 2.7 的实践心得
- Python2.x与3.x的部分区别(根据实践持续更新)
- 《python机器学习及实践-从零开始通往kaggle竞赛之路(代码Python 3.6 版)》chapter2.1.1.2
- 机器学习算法与Python实践(4) - 缩减方法(岭回归/逐步回归)
- 【Python】Django 时间字段 最佳实践
- python 购物车菜单
- python实践设计模式(二)Builder,Singleton,Prototype
- 机器学习算法与Python实践之(五)k均值聚类(k-means)
- 《python数据挖掘入门与实践》笔记1
- Python程序设计与算法基础教程·第四章上机实践
- Docker实践:运行Python应用
- python爬虫实践--求职Top10城市
- Python爬虫小实践:爬取任意CSDN博客所有文章的文字内容(或可改写为保存其他的元素),间接增加博客访问量