# -*- 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.购物车演练的更多相关文章

  1. python菜鸟学习心得

    禁忌:学习没精力,就是没精打采.没有热情. 禁忌:学习一半,然后,放在一边. 禁忌:不要东一榔头,西一棒锤. 禁忌:学习要用心. 激情是动力,专注是效率 每次学习都是绕着网络转了一圈.还是要一步一个脚 ...

  2. head first python菜鸟学习笔记(第六章)

    1. Python提供字典,允许有效组织数据,将数据与名关联,从而实现快速查找,而不是以数字关联. 字典是内置数据结构,允许将数据与键而不是数字关联.这样可以使内存中的数据与实际数据的结构保持一致.? ...

  3. head first python菜鸟学习笔记(第三章)

    1.os.chdir()切换到指定目录下,os.getcwd(),得到当前目录. >>> import os>>> os.chdir('D:\\CodeDocume ...

  4. head first python菜鸟学习笔记(第七章) ——web应用之为数据建模

    问题1. #意思是从athletelist.py中导入AthleteListfrom athletelist import AthleteList 源程序代码 import pickle from a ...

  5. head first python菜鸟学习笔记(第四章)

    1,p124,错误:NameError: name 'print_lol' is not defined 要想文件内如图显示,需要把调用BIF print()改为调用第二章的nester模块中的pri ...

  6. Python菜鸟快乐游戏编程_pygame(6)

    Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...

  7. Day2 python基础学习

    http://www.pythondoc.com/ Python中文学习大本营 本节内容: 一.字符串操作 二.列表操作 三.元组操作 四.字典操作 五.集合操作 六.字符编码操作 一.字符串操作 1 ...

  8. Python菜鸟快乐游戏编程_pygame(1)

    Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...

  9. Day2 Python基础学习——字符串、列表、元组、字典、集合

    Python中文学习大本营:http://www.pythondoc.com/ 一.字符串操作 一.用途:名字,性格,地址 name = 'wzs' #name = str('wzs')print(i ...

  10. Python入门学习指南

    对于初学者,入门至关重要,这关系到初学者是从入门到精通还是从入门到放弃.以下是结合Python的学习经验,整理出的一条学习路径,主要有四个阶段 NO.1 新手入门阶段,学习基础知识 总体来讲,找一本靠 ...

随机推荐

  1. requests库获取指定关键词的图片,并保存到本地

    var code = "45f4267f-9476-44ca-ac68-6d32aad2d4e1" 源码地址:https://gitee.com/myrensheng/data_a ...

  2. Kongmaster

    圣人处无为之事 大智若愚 穷则独善其身,达则兼济天下

  3. Account Manager privacy agreement

    Account Manager privacy agreement [Account Manager] (hereinafter referred to as "we") )We ...

  4. Generamba构建模板,让开发变得更高效

    Generamba Generamba:是为使用 Xcode 而设计的代码生成器.它主要设计用于生成 VIPER 模块,但很容易自定义模板以生成任何其他模板(在 Objective-C 和 Swift ...

  5. 【离线数仓环境搭建】安装linux及安装JDK

    创建新的虚拟机 配置网络 安装vim sudo yum install -y vim 修改免密 sudo vim /etc/sudoers (执行安装命令的时候,不再需要输入密码) 查看防火墙状态 s ...

  6. Arch安装记录(BIOS+GPT)

    尝试了下arch安装,并且尝试了下不长用的BIOS + GPT组合.都说arch的wiki强,确实很强,可惜自己的水平看不了多少. https://wiki.archlinux.org/index.p ...

  7. Darknet网络代码

    Darknet网络代码  import math from collections import OrderedDict import torch import torch.nn as nn impo ...

  8. Java基础__06.注解和反射

    注解 什么是注解? 1.注解:annocation,是从JDK5.0开始引进入的新技术. 2.注解的作用: 不是程序本身,可以对程序做出解释,这一点和注释一样: 可以被其他程序读取,这点是和注释的区别 ...

  9. unity Android 可后台替换图片

    using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; usin ...

  10. linux 安装goland

    一.Goland-IDEA 2020.3.2安装 1 下载 下载GoLand https://www.jetbrains.com/go/download/#section=linux 2 安装Gola ...