Python学习第二天-编写购物车
需求:1.启动程序后,让用户输入工资,然后打印商品列表
2.允许用户根据商品编号购买商品
3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4.可随时退出,退出时,打印已购买商品和余额
# Author: zfh
#-*-coding:utf-8-*-
product_list = [
('Oracle book',100),
('pencil', 10),
('rule',100),
('iphone',2000),
('box',200)
]
shopping_list = []
shopping_cost = 0
shopping_num = 0
i = 0
while i < 3 :
salary = input("Input your salary:")
if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(product_list):
print(index,item)
user_choice = input("Please choice >>>:")
if user_choice.isdigit():
user_choice = int(user_choice) if user_choice < len(product_list) and user_choice >= 0:
p_item = product_list[user_choice]
if p_item[1] <= salary:
shopping_list.append(p_item)
salary -= p_item[1]
shopping_cost += p_item[1]
shopping_num += 1
print("Added %s into shopping cart,your current balance is \033[31;1m %s \033[0m" %(p_item,salary))
else:
print("\033[41;1m你的余额只剩[%s]" % salary)
else:
print("商品不存在,请重新选择!".center(50,'-'))
elif user_choice == 'q':
print("shopping_list".center(50,'-'))
for p in shopping_list:
print(p)
print("共花费 %s 元," % shopping_cost,"共购买件 %s 商品" % shopping_num)
print("Your current balance:",salary)
exit()
else:
print("invalid option".center(50,'-'))
else:
print("输入错误,请重新输入金额".center(50,'-'))
i += 1
Python学习第二天-编写购物车的更多相关文章
- Python学习(一):编写购物车
1.购物车流程图: 2.代码实现: #!/usr/bin/env python #coding=utf-8 ChoiceOne =''' 1.查看余额 2.购物 3.退出 ''' ChoiceTwo ...
- Python学习第二天-编写三级菜单
编写三级菜单:1. 运行程序输出第一级菜单2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单3. 菜单数据保存在文件中4. 让用户选择是否要退出5. 有返回上一级菜单的功能 # Author: z ...
- python学习第二讲,pythonIDE介绍以及配置使用
目录 python学习第二讲,pythonIDE介绍以及配置使用 一丶集成开发环境IDE简介,以及配置 1.简介 2.PyCharm 介绍 3.pycharm 的安装 二丶IDE 开发Python,以 ...
- python学习第二次笔记
python学习第二次记录 1.格式化输出 name = input('请输入姓名') age = input('请输入年龄') height = input('请输入身高') msg = " ...
- Python学习-第二天-字符串和常用数据结构
Python学习-第二天-字符串和常用数据结构 字符串的基本操作 def main(): str1 = 'hello, world!' # 通过len函数计算字符串的长度 print(len(str1 ...
- python学习第二天 -----2019年4月17日
第二周-第02章节-Python3.5-模块初识 #!/usr/bin/env python #-*- coding:utf-8 _*- """ @author:chen ...
- Python学习笔记-练习编写ATM+购物车(购物商城)
作业需求: 模拟实现一个ATM + 购物商城程序: 1.额度 15000或自定义 2.实现购物商城,买东西加入 购物车,调用信用卡接口结账 3.可以提现,手续费5% 4.支持多账户登录 5.支持账户间 ...
- Python学习第二节——基础知识
# !/usr/bin/edv python 脚本语言的第一行,目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它.# -*- coding:UTF-8 -*- 标明编码注释 ...
- Python 学习第二章
本章内容 数据类型 数据运算 表达式 if ...else 语句 表达式 for 循环 表达式 while 循环 一.数据类型 在内存中存储的数据可以有多种类型. 在 Python 有五个标准的数据类 ...
随机推荐
- BZOJ 2118 墨墨的等式 (同余最短路)
题目大意:已知B的范围,求a1x1+a2x2+...+anxn==B存在非负正整数解的B的数量,N<=12,ai<=1e5,B<=1e12 同余最短路裸题 思想大概是这样的,我们选定 ...
- JS冒泡排序方法
- cogs 2060. 除法表达式
2060. 除法表达式 ★★ 输入文件:baoquansl.in 输出文件:baoquansl.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 经过无尽的蘑菇和 ...
- Android native CursorWindow数据保存原理
我们通过Uri查询数据库所得到的数据集,保存在native层的CursorWindow中.CursorWindow的实质是共享内存的抽象,以实现跨进程数据共享.共享内存所採用的实现方式是文件映射. 在 ...
- OpenCV特征点检測------Surf(特征点篇)
Surf(Speed Up Robust Feature) Surf算法的原理 ...
- C++11新特性应用--实现延时求值(std::function和std::bind)
说是延时求值,注意还是想搞一搞std::function和std::bind. 之前博客<C++11新特性之std::function>注意是std::function怎样实现回调函数. ...
- C++字符串操作笔试题第二波
//1.字符串替换空格:请实现一个函数,把字符串中的每一个空格替换成"%20". //比如输入"we are happy.".则输出"we%20are ...
- Educational Codeforces Round 6 B. Grandfather Dovlet’s calculator 暴力
B. Grandfather Dovlet’s calculator Once Max found an electronic calculator from his grandfather Do ...
- node13---node使用mongodb
01.js /** *最先的后台语言是Asp(微软的), */ var express = require("express"); //数据库引用 var MongoClient ...
- echarts中国地图
echarts中国地图效果图: =================== 需要引入echarts的js文件:(1.echarts.min.js:2.china.js) 下载地址: echarts.min ...