• python-三级菜单和购物车程序代码(补发)


      1 #!/usr/bin/env python
      2 #coding=utf-8
      3 # 需求:
      4 # 可依次选择进入各子菜单
      5 # 可从任意一层往回退到上一层
      6 # 可从任意一层退出程序
      7 # 所需新知识点:列表、字典
      8 import sys
      9 menu = {
     10     '北京':{
     11         '海淀':{
     12             '五道口':{
     13                 'soho':{},
     14                 '网易':{},
     15                 'google':{}
     16             },
     17             '中关村':{
     18                 '爱奇艺':{},
     19                 '汽车之家':{},
     20                 'youku':{},
     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                 '携程':{}
     46             }
     47         },
     48         '浦东':{},
     49     },
     50     '山东':{},
     51 }
     52 #控制停留在第一层
     53 while True:
     54     print('='*10+'省份及直辖市'+'='*10)
     55     for key in menu.keys(): #打印第一层key(北京、上海、山东)
     56         print(key)
     57     choice = input("请输入想查看的地域('q'or'Q'退出):").strip()
     58     if choice in menu.keys():
     59         #控制停留在第二层
     60         while True:
     61             print('='*20)
     62             print("%s下面的市(区)有:"%choice)
     63             for key in menu[choice].keys(): #打印第二层key(海淀...)
     64                 print(key)
     65             choice1 = input("请输入想查看的区域(输入‘q’or'Q'退出;输入‘b’or‘B’返回上级):").strip()
     66             if choice1 in menu[choice].keys():
     67                 #控制停留在第三层
     68                 while True:
     69                     print('=' * 20)
     70                     print("%s下面的区域有:" % choice1)
     71                     for key in menu[choice][choice1].keys(): #打印第三层key(五道口...)
     72                         print(key)
     73                     choice2 = input("请输入想查看的街道(输入‘q’or'Q'退出;输入‘b’or‘B’返回上级):").strip()
     74                     if choice2 in menu[choice][choice1].keys():
     75                         #控制停留在第四层
     76                         while True:
     77                             print('=' * 20)
     78                             print("%s下面的企业有:" % choice2)
     79                             for key in menu[choice][choice1][choice2].keys(): #打印第四层key(sohu...)
     80                                 print(key)
     81                             choice3 = input("输入‘q’or'Q'退出;输入‘b’or‘B’返回上级:").strip()#随时退出
     82                             if choice3 == 'q' or choice3 == 'Q':
     83                                 sys.exit(0)
     84                             elif choice3 == 'b' or choice3 == 'B':#返回上层
     85                                 break
     86                             else:
     87                                 print("无效的输入...")
     88                     elif choice2 == 'b' or choice2 == 'B':#返回上层
     89                         break
     90                     elif choice2 == 'q' or choice2 == 'Q':#随时退出
     91                         sys.exit(0)
     92                     else:
     93                         print("无效的输入...")
     94             elif choice1 == 'b' or choice1 == 'B':#返回上层
     95                 break
     96             elif choice1 == 'q' or choice1 == 'Q':#随时退出
     97                 sys.exit(0)
     98             else:
     99                 print("无效的输入...")
    100     elif choice == 'q' or choice == 'Q':#随时退出
    101         sys.exit(0)
    102     else:
    103         print("无效地址...")
    三级菜单代码
     1 #!/usr/bin/env python
     2 #coding=utf-8
     3 import sys
     4 #定义商品列表
     5 goods =[
     6 {"name":"电脑", "price": 1999},
     7 {"name":"鼠标", "price": 10},
     8 {"name":"游艇", "price": 20},
     9 {"name":"美女", "price": 998},
    10 {"name":"大宝剑","price":666}
    11 ]
    12 #定义一个余额列表
    13 balance = []
    14 #定义一个购物车列表
    15 shopping_car = []
    16 #简单定义用户名密码
    17 username = 'alex'
    18 password = '123456'
    19 #简易的登录验证
    20 i = 0
    21 while i < 3:
    22     name = input("请输入用户名:").strip()
    23     passwd = input("请输入密码:").strip()
    24     if name == username and passwd == password:
    25         print('='*15+'欢迎登陆世纪联华'+'='*15)
    26         break
    27     else:
    28         print("用户名或者密码错误,还剩%d次登陆机会!"%(2-i))
    29         i += 1
    30 else:
    31     sys.exit("三次输入失败,退出...")
    32 #打开余额文件,查看是否有余额,是则不提示输入工资
    33 with open('balance_file','r',encoding='utf-8') as balance_f:
    34     balance_list = balance_f.readlines()
    35 if len(balance_list) == 0: #如若balance_file为空,提示输入工资并作为余额
    36     salary = int(input("请输入你的工资:").strip())
    37     balance.append(salary)
    38 else:  #balance_file余额,则将余额添加到余额列表
    39     history_balance  = int(balance_list[0].strip('
    '))
    40     balance.append(history_balance)
    41 while True:
    42     #打印商品列表
    43     print('='*10+"本店有以下商品,欢迎采购"+'='*10)
    44     for g,good in enumerate(goods,1):
    45         print("33[1;31;0m%s.  %s  %s 33[0m"%(g,good["name"],good["price"]))
    46     #获取用户输入
    47     action = input("请选择购买的商品编号(退出请按q;查询历史购买记录请按f):").strip()
    48     #商品购买
    49     if action.isdigit(): #检测输入的字符串是否只由数字组成
    50         action = int(action) #若字符串只有数字组成,将其转换成int类型
    51         if action > 0 and action <= len(goods):#判断输入的商品编号是否在商品列表内
    52             if balance[0] >= goods[action-1]["price"]:#判断余额是否充足
    53                 shopping_car.append(goods[action-1])  #记录购物车
    54                 history_data = goods[action-1]["name"]+':'+ str(goods[action-1]["price"])+'
    '
    55                 with open('history_file','a',encoding='utf-8') as h_f: #将选购商品信息写入history_file作为历史清单
    56                     h_f.write(history_data)
    57                 balance[0] = balance[0] - goods[action-1]["price"] #余额扣款
    58                 print("33[1;0m%s已购买成功!33[0m"%(goods[action-1]["name"]))
    59             else:
    60                 print("33[1;0m余额已不足...33[0m")
    61         else:
    62             print("33[1;0m此商品不存在,请根据商品列表购买!33[0m")
    63     #用户退出
    64     elif action == 'q' or action == 'Q':
    65         if len(shopping_car) > 0:
    66             print('='*15+"您已购买了以下商品:")
    67         else:
    68             print("您没有购买商品..")
    69         for g,good in enumerate(shopping_car,1): #遍历购物车,打印购买商品
    70             print("33[1;35;0m%s.  %s  %s 33[0m"%(g,good["name"],good["price"]))
    71         print("33[1;0m您的余额为:%d,欢迎下次光临!"%(balance[0])) #打印余额
    72         with open('balance_file','r+',encoding='utf-8') as b_f: #将余额覆盖到balance_file
    73             b_f.write(str(balance[0])+'
    ')
    74         sys.exit(0)
    75     #查询历史消费记录
    76     elif action == 'f' or action == 'F':
    77         with open('history_file','r',encoding='utf-8') as h_f:
    78             h_list = h_f.readlines()  #逐行读取history_file
    79         if len(h_list) == 0:
    80             print("没有历史购买记录!")
    81         else:
    82             print("33[1;0m以下为历史购买记录:33[0m") #遍历h_list,打印历史清单
    83             for i,h in enumerate(h_list,1):
    84                 (name,price) = h.strip('
    ').split(":")
    85                 print("33[1;35;0m%s.  %s  %s33[0m"%(i,name,price))
    购物车程序代码
  • 相关阅读:
    lazarus中objfpc编译模式与delphi编译模式的不同
    TFDUpdateOptions.UpdateNonBaseFields
    protobuf数据类型与delphi数据类型映射
    delphi基于google protobuf开发
    mormot https设置
    基于数据模型的序列
    fastmove+fastcode
    localstack 应用架构
    dremio 21 pdfs 不在支持
    dremio 21 其他一些新特性
  • 原文地址:https://www.cnblogs.com/alvin-jie/p/8393904.html
Copyright © 2020-2023  润新知