python—三级菜单,购物车

一、三级菜单:

menu = {
    '北京':{
        '海淀':{
            '五道口':{
                'soho':{},
                '网易':{},
                'google':{}
            },
            '中关村':{
                '爱奇艺':{},
                '汽车之家':{},
                'youku':{},
            },
            '上地':{
                '百度':{},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '北航':{},
            },
            '天通苑':{},
            '回龙观':{},
        },
        '朝阳':{},
        '东城':{},
    },
    '上海':{
        '闵行':{
            "人民广场":{
                '炸鸡店':{}
            }
        },
        '闸北':{
            '火车战':{
                '携程':{}
            }
        },
        '浦东':{},
    },
    '山东':{},
}


需求:

  • 可依次选择进入各子菜单
  • 可从任意一层往回退到上一层
  • 可从任意一层退出程序
current_layer=menu
layer=[]
while True:
    for i in current_layer:
        print(i)
    n1=input('>:').strip()
    if not n1: continue
    if n1 in current_layer:
        layer.append(current_layer)
        # print(layer)
        current_layer=current_layer[n1]
    elif n1=='b':
        if len(layer)!=0:
            current_layer = layer.pop()
        else:
            print('已经是顶层了')
    elif n1=='q':
        exit()

二、购物车程序:

数据结构:
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
......
]
功能要求:

1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表

2、允许用户根据商品编号购买商品

3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒

4、可随时退出,退出时,打印已购买商品和余额

5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示

扩展需求:

1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买

2、允许查询之前的消费记录

li = [{"name": "电脑", "price": 1999},
      {"name": "鼠标", "price": 10},
      {"name": "游艇", "price": 20},
      {"name": "美女", "price": 998}]
shopping_car = []
user_name = 'alex'
password = '123'
money = 0
exit_flag = False

count = 0
while count < 3:
    name = input('用户名:')
    key = input('密码:')
    if name == user_name and key == password:
        wage = input('输入你的工资:')
        money = wage
        break
    else:
        print('你的用户名或密码错误')
        # 到这里实现第一步
        count += 1
    if count == 3:
        exit()
while not exit_flag:
    print('商品列表'.center(30, '-'))
    for index, i in enumerate(li):
        print('%s.  %s %s' % (index+1, i['name'], i['price']))
    num = input('你想买的商品编号:(输入q可退出)')
    if num.isdigit():
        num = int(num)
        if (0 < num) and num <= len(li):
            shopping_car.append(li[num-1])
            print('你选择的商品%s已加入购物车' % shopping_car)
            if int(money)-li[num-1]['price'] > 0:
                money = int(money)-li[num-1]['price']
                print('账户余额为%s' % money)
            else:
                print('你的余额不足!')
        else:
            print('你选择的商品不存在!')
            # 到这里实现第二步以及第三步
    elif num == 'q':
        if len(shopping_car) > 0:
            print('以下是您买的商品以及账户余额:')
            for index, i in enumerate(shopping_car):
                print('%s.  %s %s' % (index + 1, i['name'], i['price']))
            print('你的账户余额为%s' % money)
            # 实现第四步
            exit_flag = True
        else:
            break

  

li = [{"name": "电脑", "price": 1999},
      {"name": "鼠标", "price": 10},
      {"name": "游艇", "price": 20},
      {"name": "美女", "price": 998}]
f = open('D:/购物车/sa.txt', 'r', encoding='utf-8')
f1 = open('D:/购物车/xiaobai2.txt', 'r', encoding='utf-8')
users_buy = f.readlines()
users_info = f1.readlines()
shopping_car = []
user_name = 'alex'
password = '123'
money = 0
exit_flag = False
count = 0
while count < 3:
    name = input('用户名:')
    key = input('密码:')
    f = open('D:/购物车/sa.txt', 'r', encoding='utf-8')

    print(users_buy)
    f.close()
    if name == user_name and key == password:
        wage = input('输入你的工资:')
        money = wage
        break
    else:
        print('你的用户名或密码错误')
# 到这里实现输入用户名和密码后输入工资的功能
        count += 1
    if count == 3:
        exit()
while not exit_flag:
    print('商品列表'.center(30, '-'))
    for index, i in enumerate(li):
        print('%s.  %s %s' % (index + 1, i['name'], i['price']))
    num = input('你想买的商品编号:(输入q可退出)')
    if num.isdigit():
        num = int(num)
        if (0 < num) and num <= len(li):
            shopping_car.append(li[num - 1])
            print('你选择的商品%s已加入购物车' % shopping_car)
            f = open('D:/购物车/sa.txt', 'a', encoding='utf-8')
            f.write(li[num - 1]['name'])
            f.close()
            if int(money) - li[num - 1]['price'] > 0:
                money = int(money) - li[num - 1]['price']
                print('账户余额为%s' % money)
            else:
                print('你的余额不足!')
        else:
            print('你选择的商品不存在!')
# 允许用户根据商品编号购买商品
# 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
    elif num == 'q':
        if len(shopping_car) > 0:
            print('以下是您买的商品以及账户余额:')
            for index, i in enumerate(shopping_car):
                print('%s.  %s %s' % (index + 1, i['name'], i['price']))
            print('你的账户余额为%s' % money)
# 可随时退出,退出时,打印已购买商品和余额
# 在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
            f1 = open('D:/购物车/xiaobai2.txt', 'a', encoding='utf-8')
            f = open('D:/购物车/sa.txt', 'a', encoding='utf-8')
            f.write(str(money))
            f1.write(str(shopping_car))
            f1.close()
            f.close()
            exit_flag = True
        else:
            exit('已退出购买!')

  



原文地址:https://www.cnblogs.com/xiaobai-yemao/p/8577698.html