python菜鸟学习: 4.购物车演练
# -*- coding: utf-8 -*-
'''
1.启动程序后让用户输入消费额,然后打印商品列表'
2.允许用户根据商品编码购买商品
3.用户选择商品后,检测余额是否够,够则直接扣款,不够就提醒
4.可以随时退出,退出时,打印已购买的商品和余额
'''
# 定义商品列表
global commodList
commodList = {"笔记本": [["华为", 1000, 11], ["戴尔", 2000, 12], ["外星人", 3000, 13], ["联想", 500, 14]], \
"手机": [["华为", 999, 21], ["小米", 888, 22], ["苹果", 777, 23], ["oppo", 666, 24]], \
"冰箱": [["格力", 2222, 31], ["海尔", 1111, 32], ["海信", 3333, 33], ["奥克斯", 4444, 34]], \
"电视": [["长虹", 5555, 41], ["海信", 6666, 42], ["小米", 8888, 43], ["夏新", 3333, 44]]}
# 得到用户余额
def getUserMoney():
money = input("请输入您的余额:\n")
if money.isdigit():
money=int(money)
return money
else:
print("请输入一个整数")
exit()
# 显示物品菜单
def showCommodity():
print("欢迎来到新时代购物广场:您可以任意限购以下商品:", end="\n")
for commodType in commodList.keys():
print(commodType + ":", end="\n")
commodName = commodList[commodType]
# print(commodName) //[['华为', 1000], ['戴尔', 2000], ['外星人', 3000], ['联想', 500]]
for commodName1 in commodName:
print("品牌:\033[32;1m {_commodName}\033[0m<---->价格:{_commodPrice}<---->商品编码:{_commodNumber}" \
.format(_commodName=commodName1[0], _commodPrice=commodName1[1], \
_commodNumber=commodName1[2]))
# 根据商品编码得到商品价格及商品名称
def getCommdityPrice(commodNum):
# 判断商品是否存在
if commodNum != 99999:
flag = False
# 根据物品编码得到物品
for commodName in commodList.keys():
for commodNum1 in commodList[commodName]:
if commodNum1[2] == commodNum:
flag = True
return commodNum1[1], commodNum1[0]
if flag == False:
print("\033[31;1m{_commodNum}商品编码不存在\033[0m".format(_commodNum=commodNum))
exit()
else:
print("结束购物!!")
# 增加购物车
def shoppingGet():
# 定义用户余额
UserMoney1 = getUserMoney()
# 定义用户购买的物品列表
UserCommList = []
showCommodity()
while True:
# 购买商品列表
shoppNum = int(input("请选择你想购买的物品编码,如果输入 99999 则表示本次选购已完成!:\n"))
# 商品名称和商品价格
try:
commodNum = getCommdityPrice(shoppNum)[1]
money1 = int(getCommdityPrice(shoppNum)[0])
except Exception as e:
print("")
if shoppNum == 99999:
print(UserCommList, UserMoney1)
UserCommListName = []
for commodNo in UserCommList:
UserCommListName.append(getCommdityPrice(commodNo)[1])
print(
"您现在购买的商品:{_UserCommList},余额为:{_UserMoney1}".format(_UserCommList=UserCommListName,
_UserMoney1=UserMoney1))
print()
exit()
# 判断money够用
elif money1 <= UserMoney1:
# 如果足够则把商品加到购物车列表中
UserCommList.append(shoppNum)
# 增加后,扣款
UserMoney1 = UserMoney1 - money1
print(
"您现在购物车:{_UserCommList},余额为:{_UserMoney1}".format(_UserCommList=UserCommList, _UserMoney1=UserMoney1))
# 判断money不够用
else:
# 如果余额不足以购买则输出
print("您的余额为{_Money},不足以购买<{_commodNum}>".format(_Money=UserMoney1, _commodNum=commodNum))
if __name__ == '__main__':
shoppingGet()
python菜鸟学习: 4.购物车演练的更多相关文章
- python菜鸟学习心得
禁忌:学习没精力,就是没精打采.没有热情. 禁忌:学习一半,然后,放在一边. 禁忌:不要东一榔头,西一棒锤. 禁忌:学习要用心. 激情是动力,专注是效率 每次学习都是绕着网络转了一圈.还是要一步一个脚 ...
- head first python菜鸟学习笔记(第六章)
1. Python提供字典,允许有效组织数据,将数据与名关联,从而实现快速查找,而不是以数字关联. 字典是内置数据结构,允许将数据与键而不是数字关联.这样可以使内存中的数据与实际数据的结构保持一致.? ...
- head first python菜鸟学习笔记(第三章)
1.os.chdir()切换到指定目录下,os.getcwd(),得到当前目录. >>> import os>>> os.chdir('D:\\CodeDocume ...
- head first python菜鸟学习笔记(第七章) ——web应用之为数据建模
问题1. #意思是从athletelist.py中导入AthleteListfrom athletelist import AthleteList 源程序代码 import pickle from a ...
- head first python菜鸟学习笔记(第四章)
1,p124,错误:NameError: name 'print_lol' is not defined 要想文件内如图显示,需要把调用BIF print()改为调用第二章的nester模块中的pri ...
- Python菜鸟快乐游戏编程_pygame(6)
Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...
- Day2 python基础学习
http://www.pythondoc.com/ Python中文学习大本营 本节内容: 一.字符串操作 二.列表操作 三.元组操作 四.字典操作 五.集合操作 六.字符编码操作 一.字符串操作 1 ...
- Python菜鸟快乐游戏编程_pygame(1)
Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...
- Day2 Python基础学习——字符串、列表、元组、字典、集合
Python中文学习大本营:http://www.pythondoc.com/ 一.字符串操作 一.用途:名字,性格,地址 name = 'wzs' #name = str('wzs')print(i ...
- Python入门学习指南
对于初学者,入门至关重要,这关系到初学者是从入门到精通还是从入门到放弃.以下是结合Python的学习经验,整理出的一条学习路径,主要有四个阶段 NO.1 新手入门阶段,学习基础知识 总体来讲,找一本靠 ...
随机推荐
- MOBI 】逆流而上的我:重塑心理韧性,在逆境中实现自我超越【早稻田大学名誉教授、哈佛大学客座研究
书本详情 标题:逆流而上的我:重塑心理韧性,在逆境中实现自我超越[早稻田大学名誉教授.哈佛大学客座研究员加藤谛三的年度心理学温情巨作!掌握心理韧性,每个人都是生活的修行者!] | 加藤谛三 |年份:2 ...
- 错误 C2664 “int fputs(const char *,FILE *)”: 无法将参数 1 从“char”转换为“const char *”解决方法
遇到这个问题,请打开本项目的Properties(属性) -------> Configuration Properties(配置属性) -------->General(常规) ---- ...
- ubuntu亲测安装opencv和成功解决Makefile:160: recipe for target 'all' failed make: *** [all] Error 2
1.因为项目需要,我安装的是opencv3.0.0,从github上面下载的opencv包 git clone https://github.com/Itseez/opencv.git git clo ...
- Python安装及配置教程
安装教程 一.python3.6安装步骤 1.首先我们移步官网,下载最新版本的python-3.6.0.点我,我把你传送到python官网 在DownLoad下拉框中点击Windows,选择要下载的文 ...
- vue+vant-ui小程序,微信小程序自定义导航栏(适配刘海屏)
整理一下微信小程序自定义导航栏和刘海屏适配问题 1.首先在根据官方文档,我们在小程序修改 app.json 中的 window 的属性 "navigationStyle": &qu ...
- -bash: pip: command not found
使用pip安装软件包时报错命令不存在 [root@test ~]# pip -V -bash: pip: command not found 机器上没有安装pip,需要手动进行安装 centos系统: ...
- vscore 中 vim 常用快捷键
谷歌浏览器 ctrl + T 新建一个页面 ctrl + J 查看下载界面 F6 直接搜索 vscore 在 vscore 中使用 vim 建议去掉 ctrl 键的功能捆绑,不然会覆盖掉很多的 vsc ...
- FFmpeg转换直播流格式
mp4转rtsp ffmpeg -re -i 1671680590843.mp4 -vcodec copy -acodec copy -f rtsp rtsp://localhost:8554/liv ...
- 根据两点经纬度计算两点间距离 js
getDistance(lat1, lng1, lat2, lng2) { let radLat1 = lat1 * Math.PI / 180.0; let radLat2 = lat2 * Mat ...
- execsnoop
作用 execsnoop通过ftrace实时监控进程的exec()行为,输出短时进程的信息,包括进程 PID.父进程 PID.命令行参数以及执行的结果. 安装execsnoop git clone - ...