product_list = [
['Iphone',5888],
['Mac Air',8000],
['XiaoMi',19.9],
['coffee',30],
['Tesla',820000],
['Bike',700],
['Cloth',200], ]
shop_car={}
salary=int(input("请输入您的工资: "))
while True:
index=0
for i in product_list:
print(index,i)
index+=1
choice=input("请输入商品编号 w保存,q 退出: ")
if choice.isdigit():
choice=int(choice)
if choice > 0 and choice <= len(product_list):
product=product_list[choice]
if salary >= product[1]:
if product[0] in shop_car: #已经购买过的商品, 键值对,键:商品 , 值:【数量 价格】
shop_car[product[0]][1] += 1
else:
shop_car[product[0]] = [product[1] ,1] #购物车里没有的商品 添加一条键值对,键:商品 值:【数量:值:价格】
salary -= product[1] #{'XiaoMi': [19.9, 2], 'coffee': [30, 1]}
else:
print("余额不足")
else:
print("您输入的商品编号不存在")
elif choice == 'w':
print("--------------已经购买的商品列表----------------") #{'XiaoMi': [19.9, 2], 'coffee': [30, 1]}
for k in shop_car:
print( "商品:%5s 单价:%5s 数量:%5s 总价:%5s " %
(k,
shop_car[k][0],
shop_car[k][1],
shop_car[k][0]* shop_car[k][1]) )
print("您的余额 %s" % salary)
print("end-----------")
break
else:
exit("退出程序")

三级菜单

 menu = {
'北京':{
'海淀':{
'五道口':{
'soho':{},
'网易':{},
'google':{}
},
'中关村':{
'爱奇艺':{},
'汽车之家':{},
'youku':{},
},
'上地':{
'百度':{},
},
},
'昌平':{
'沙河':{
'钓鱼岛':{},
'北航':{},
},
'天通苑':{},
'回龙观':{},
},
'朝阳':{},
'东城':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{}
}
},
'闸北':{
'火车战':{
'携程':{}
}
},
'浦东':{},
},
'河北':{},
} while True:#一直打印一级菜单
for key in menu: #打印一级菜单的 key
print(key)
choice=input(">")
if len(choice)==0:continue
if choice == 'b': break
if choice=='q':exit('退出程序')
while True: #一直循环打印二级菜单
lary=menu[choice] #进入二级菜单
for key in lary:
print(key)
choice1=input(">>")
if len(choice)==0:continue
if choice1 == 'b': break
if choice1 == 'q': exit('退出程序')
if choice1 in lary:
while True:
lary2=lary[choice1] #进入三级菜单
for key in lary2:
print(key)
choice2=input(">>>")
if len(choice2)==0: continue
if choice2 == 'b': break
if choice2 == 'q': exit('退出程序')
if choice2 in lary2:
while True:
lary3= lary2[choice2]
for key in lary3:
print(key)
endchoice=input('b 返回上一层,q退出: ')
if endchoice == 'b': break
if endchoice == 'q': exit('退出程序')

三级菜单改进版

 menu = {
'北京':{
'海淀':{
'五道口':{
'soho':{},
'网易':{},
'google':{}
},
'中关村':{
'爱奇艺':{},
'汽车之家':{},
'youku':{},
},
'上地':{
'百度':{},
},
},
'昌平':{
'沙河':{
'钓鱼岛':{},
'北航':{},
},
'天通苑':{},
'回龙观':{},
},
'朝阳':{},
'东城':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{}
}
},
'闸北':{
'火车战':{
'携程':{}
}
},
'浦东':{},
},
'山东':{},
}
last_layers=[menu]
current_layer=menu
while True:
for key in current_layer:
print(key)
choice=input(">")
if len(choice)==0:continue
if choice in current_layer: #列表套字典
current_layer=current_layer[choice]
last_layers=last_layers.append(current_layer) #列表添加进 本层 choice键的 内容
if choice == 'b':
current_layer=last_layers[-1]
last_layers.pop()# l=[]
if current_layer == 'q':
break # me=menu['北京']
# print(me)
# l=l.append(me)
# for k in l:
# print(l)

python if、elif的区别

elif配合if使用如果if中条件不成立执行elif,如果if条件成立,elif不会执行,注意即使上个if条件判断成立,它下面的if条件如果也成立依然会执行;

a='zhanggen'

if 'a' in a:
print('ok1') elif 'h' in a:
print('ok2') if 'g' in a:
print('ok3')

a='zhanggen'
if 's' in a:
print('ok1') elif 'h' in a:
print('ok2') elif 'g' in a: #只会执行1个
print('ok3')

Python购物车的更多相关文章

  1. 简单的python购物车

                 这几天,一直在学python,跟着视频老师做了一个比较简单的python购物车,感觉不错,分享一下 products = [['Iphone8',6888],['MacPro ...

  2. python 购物车小程序

    python 购物车小程序 功能要求:1.启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表2.允许用户根据商品编号购买商品3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒4. ...

  3. python购物车小案例

    python购物车小案例# 案列描述:有一个小型水果店里面有水果(苹果:¥8/kg,香蕉:¥5/kg,芒果:¥15/kg,葡萄:¥12/kg),客户带了100元钱进店选购水果.# 1.客户输入相应序号 ...

  4. python 购物车和三级菜单

    程序:购物车程序 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时退出,退出时,打印已购买商品和余额 ...

  5. 5th,Python购物车模拟

    1.启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 4.可随时退出,退 ...

  6. python 购物车小程序(列表、循环、条件语句)

    goods = [ ['iphone6s', 5800], ['mac book', 9000], ['coffee', 32], ['python book', 80], ['bicyle', 15 ...

  7. python购物车系统

    购物车系统模拟:product_list = [ ('java',100), ('python',200), ('键盘',500), ('电脑',4000), ('mac Book',7000),]S ...

  8. python购物车练习题

    # 购物车练习# 1.启动程序后,让用户输入工资,打印商品列表# 2.允许用户根据商品编号购买商品# 3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒# 4.可随时退出,退出时,打印已购买 ...

  9. Python购物车的实现课程

    需求: 1.用户输入工资收入 2.打印商品列表 3.用户选择商品,不断的加入购物车 4.检测用户余额,直接捐款,不足提示余额不足 5.允许主动退出,退出时,打印已购商品列表 重点方法: 打印列表下标的 ...

随机推荐

  1. python学习 day011打卡 迭代器

    本节的主要内容: 1.函数名的使用以及第一类对象 2.闭包 3.迭代器 一.函数名的运用. 函数名是一个变量,但它是一个特殊的变量,与括号配合可以执行函数的变量. 1.函数名的内存地址 def fun ...

  2. 遍历一个可迭代对象中的所有元素,但是却不想使用for循环

    def manual_iter(): with open('/etc/passwd') as f: try: while True: line = next(f) print(line, end='' ...

  3. C#连接数据库open函数失败

    错误信息:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provider ...

  4. react面试问题总结

    1. 在生命周期中的哪一步你应该发起 AJAX 请求? 我们应当将AJAX 请求放到 componentDidMount 函数中执行,主要原因有下: 放到componentWillMount不好.  ...

  5. Anroid 搭建Maven私服(Android Studio)

    一.场景 ① 公司中有多个项目,多人开发,开发中的某一模块需要被其他几个项目的模块所依赖,最简单粗暴的方式就是打包成aar或者jar文件逐个拷贝到libs下进行依赖,可是这种方式太过于麻烦,而且需要每 ...

  6. 大数据新手之路三:安装Kafka

    Ubuntu16.04+Kafka1.0.0 1.下载kafka_2.11-1.0.0.tgz http://kafka.apache.org/downloads 2.解压到/usr/local/ka ...

  7. maven配置环境变量失败解决办法

    配置maven路径什么的统统正确,最后测hi不成功.在网上搜索了好多资料方法都解不了 具体问题具体对待吧,如果有和我类似的小伙伴,可以尝试一下我的这个办法,在maven路径后面加/bin path变量 ...

  8. Python pymysql 增删改查封装

    关于pymysql 的增删改查,简单做个封装,方便后面使用直接拿来调用即可. 其中 增删改 的处理其实是一致的,本可以使用统一的方法,但是为了明显区分,这里分开来写了. 直接看代码就即可,如下: # ...

  9. fMRI在认知心理学上的研究

    参考:Principles of fMRI 1 问题: 1. fMRI能做什么不能做什么? 第一周:fMRI简介,data acquisition and reconstruction 大致分为两类: ...

  10. every day a practice —— morning(3)

    "WeChat does not store any chat histories. They are stored only on users' phones, computers or ...