1. product_list = [
  2. ['Iphone',5888],
  3. ['Mac Air',8000],
  4. ['XiaoMi',19.9],
  5. ['coffee',30],
  6. ['Tesla',820000],
  7. ['Bike',700],
  8. ['Cloth',200], ]
  9. shop_car={}
  10. salary=int(input("请输入您的工资: "))
  11. while True:
  12. index=0
  13. for i in product_list:
  14. print(index,i)
  15. index+=1
  16. choice=input("请输入商品编号 w保存,q 退出: ")
  17. if choice.isdigit():
  18. choice=int(choice)
  19. if choice > 0 and choice <= len(product_list):
  20. product=product_list[choice]
  21. if salary >= product[1]:
  22. if product[0] in shop_car: #已经购买过的商品, 键值对,键:商品 , 值:【数量 价格】
  23. shop_car[product[0]][1] += 1
  24. else:
  25. shop_car[product[0]] = [product[1] ,1] #购物车里没有的商品 添加一条键值对,键:商品 值:【数量:值:价格】
  26. salary -= product[1] #{'XiaoMi': [19.9, 2], 'coffee': [30, 1]}
  27. else:
  28. print("余额不足")
  29. else:
  30. print("您输入的商品编号不存在")
  31. elif choice == 'w':
  32. print("--------------已经购买的商品列表----------------") #{'XiaoMi': [19.9, 2], 'coffee': [30, 1]}
  33. for k in shop_car:
  34. print( "商品:%5s 单价:%5s 数量:%5s 总价:%5s " %
  35. (k,
  36. shop_car[k][0],
  37. shop_car[k][1],
  38. shop_car[k][0]* shop_car[k][1]) )
  39. print("您的余额 %s" % salary)
  40. print("end-----------")
  41. break
  42. else:
  43. exit("退出程序")

三级菜单

  1. menu = {
  2. '北京':{
  3. '海淀':{
  4. '五道口':{
  5. 'soho':{},
  6. '网易':{},
  7. 'google':{}
  8. },
  9. '中关村':{
  10. '爱奇艺':{},
  11. '汽车之家':{},
  12. 'youku':{},
  13. },
  14. '上地':{
  15. '百度':{},
  16. },
  17. },
  18. '昌平':{
  19. '沙河':{
  20. '钓鱼岛':{},
  21. '北航':{},
  22. },
  23. '天通苑':{},
  24. '回龙观':{},
  25. },
  26. '朝阳':{},
  27. '东城':{},
  28. },
  29. '上海':{
  30. '闵行':{
  31. "人民广场":{
  32. '炸鸡店':{}
  33. }
  34. },
  35. '闸北':{
  36. '火车战':{
  37. '携程':{}
  38. }
  39. },
  40. '浦东':{},
  41. },
  42. '河北':{},
  43. }
  44.  
  45. while True:#一直打印一级菜单
  46. for key in menu: #打印一级菜单的 key
  47. print(key)
  48. choice=input(">")
  49. if len(choice)==0:continue
  50. if choice == 'b': break
  51. if choice=='q':exit('退出程序')
  52. while True: #一直循环打印二级菜单
  53. lary=menu[choice] #进入二级菜单
  54. for key in lary:
  55. print(key)
  56. choice1=input(">>")
  57. if len(choice)==0:continue
  58. if choice1 == 'b': break
  59. if choice1 == 'q': exit('退出程序')
  60. if choice1 in lary:
  61. while True:
  62. lary2=lary[choice1] #进入三级菜单
  63. for key in lary2:
  64. print(key)
  65. choice2=input(">>>")
  66. if len(choice2)==0: continue
  67. if choice2 == 'b': break
  68. if choice2 == 'q': exit('退出程序')
  69. if choice2 in lary2:
  70. while True:
  71. lary3= lary2[choice2]
  72. for key in lary3:
  73. print(key)
  74. endchoice=input('b 返回上一层,q退出: ')
  75. if endchoice == 'b': break
  76. if endchoice == 'q': exit('退出程序')

三级菜单改进版

  1. menu = {
  2. '北京':{
  3. '海淀':{
  4. '五道口':{
  5. 'soho':{},
  6. '网易':{},
  7. 'google':{}
  8. },
  9. '中关村':{
  10. '爱奇艺':{},
  11. '汽车之家':{},
  12. 'youku':{},
  13. },
  14. '上地':{
  15. '百度':{},
  16. },
  17. },
  18. '昌平':{
  19. '沙河':{
  20. '钓鱼岛':{},
  21. '北航':{},
  22. },
  23. '天通苑':{},
  24. '回龙观':{},
  25. },
  26. '朝阳':{},
  27. '东城':{},
  28. },
  29. '上海':{
  30. '闵行':{
  31. "人民广场":{
  32. '炸鸡店':{}
  33. }
  34. },
  35. '闸北':{
  36. '火车战':{
  37. '携程':{}
  38. }
  39. },
  40. '浦东':{},
  41. },
  42. '山东':{},
  43. }
  44. last_layers=[menu]
  45. current_layer=menu
  46. while True:
  47. for key in current_layer:
  48. print(key)
  49. choice=input(">")
  50. if len(choice)==0:continue
  51. if choice in current_layer: #列表套字典
  52. current_layer=current_layer[choice]
  53. last_layers=last_layers.append(current_layer) #列表添加进 本层 choice键的 内容
  54. if choice == 'b':
  55. current_layer=last_layers[-1]
  56. last_layers.pop()# l=[]
  57. if current_layer == 'q':
  58. break
  59.  
  60. # me=menu['北京']
  61. # print(me)
  62. # l=l.append(me)
  63. # for k in l:
  64. # print(l)

python if、elif的区别

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

  1. a='zhanggen'
  2.  
  3. if 'a' in a:
  4. print('ok1')
  5.  
  6. elif 'h' in a:
  7. print('ok2')
  8.  
  9. if 'g' in a:
  10. print('ok3')
  1.  
  1. a='zhanggen'
  1. if 's' in a:
  2. print('ok1')
  3.  
  4. elif 'h' in a:
  5. print('ok2')
  6.  
  7. elif 'g' in a: #只会执行1个
  8. 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. 【selenium2】【unittest】

    #栗子 from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver ...

  2. 转 这种方法可以免去自己计算大文件md5 的麻烦

    using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor;using ...

  3. CentOS下修改Apache默认端口80

    打开  /etc/httpd/conf/httpd.conf  文件 修改这个地方     #Listen 12.34.56.78:80     Listen 80 #把80改为你设置的端口,我设置端 ...

  4. ABP捕捉异常错误代码

    在服务层或者CORE层  随便哪里都可以  创建一个捕捉异常的文件夹  里面写一个LonsidException类 继承后面的接口  然后重写继承的方法  这样在ABP项目运行阶段  无论在哪里出现异 ...

  5. 力扣(LeetCode)258. 各位相加

    给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所 ...

  6. 淘宝开源的H5移动开发UI框架genie-ui

    官网地址: https://npm.taobao.org/package/genie-ui

  7. 学习笔记45—Linux压缩集

    1.压缩功能 安装 sudo apt-get install rar 卸载 sudo apt-get remove rar 2.解压功能 安装 sudo apt-get install unrar 卸 ...

  8. const修饰函数

    #include <iostream> using namespace std; class A { public: A(int age); void printAge() const; ...

  9. (转)Linux下设置和查看环境变量

    原文地址:<Linux下设置和查看环境变量> Linux的变量种类 按变量的生存周期来划分,Linux变量可分为两类: 1. 永久的:需要修改配置文件,变量永久生效. 2. 临时的:使用e ...

  10. Python PyQt5 Pycharm 环境搭建及配置

    PyQt5相关安装 python 版本 python 3.6.3  1.安装PyQt5 执行命令: pip install pyqt5 2.安装PyQt5-tools 执行命令:pip install ...