python学习之day2学习笔记(2)

五、字典

#字典格式:key-value
info={
    'stu1':"ZhangSan",
    'stu2':"LiSi",
    'stu3':"WangWu",
}
print(info)#无序输出字典:{'stu1': 'ZhangSan', 'stu2': 'LiSi', 'stu3': 'WangWu'}
print(info.keys())#输出字典的key。输出:dict_keys(['stu1', 'stu2', 'stu3'])
print(info.values())#输出字典的values。输出:dict_values(['ZhangSan', 'LiSi', 'WangWu'])
print(info['stu1'])#显示"stu1"的信息。输出:ZhangSan
print(info.get('stu5'))#查找,若无对象,输出None。输出:None
info['stu1']="张三"#更改"stu1"的信息。输出:{'stu1': '张三', 'stu2': 'LiSi', 'stu3': 'WangWu'}
print(info)
#del
info.pop("stu1")
print(info)#输出:{'stu2': 'LiSi', 'stu3': 'WangWu'}
b={
    'stu1':"ZS",
    1:3,
    2:3
}
info.update(b)#数据更新添加。输出:{'stu2': 'LiSi', 'stu3': 'WangWu', 'stu1': 'ZS', 1: 3, 2: 3}
c=dict.fromkeys([6,7,8],"test")#通过一个列表生成默认字典
print(c)#输出:{6: 'test', 7: 'test', 8: 'test'}

六、多级列表

#多级列表
info1={
    'stu1':{
        "name":["ZhangSan","张三"],
        "years":["11"],
    },
    "stu2":{
        "name":["WangWu","王五"],
        "years":["12"],
    }
}
print(info1)#输出({'stu1': {'name': ['ZhangSan', '张三'], 'years': ['11']}, 'stu2': {'name': ['WangWu', '王五'], 'years': ['12']}},)
#info1['stu1']["name"][1]="耶耶耶耶张三"#更改数据,输出:{'stu1': {'name': ['ZhangSan', '耶耶耶耶张三'], 'years': ['11']}, 'stu2': {'name': ['WangWu', '王五'], 'years': ['12']}}
#key在字典里,则不会改变数据,否则就添加
info1.setdefault("stu1",{"name":["LiSi","李四"]})
print(info1['stu1'])#输出:{'name': ['ZhangSan', '张三'], 'years': ['11']}
info1.setdefault("stu3",{"name":["LiSi","李四"]})
print(info1['stu3'])#输出:{'name': ['LiSi', '李四']}

七、购物车程序

需求:

  1. 启动程序后,让用户输入工资,然后打印商品列表
  2. 允许用户根据商品编号购买商品
  3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 
  4. 可随时退出,退出时,打印已购买商品和余额
shopping_list=[]
product_list = [["IPhone",5800],["Mac Pro",12000],["Starbuck Latte",31],["Alex Pythn",81],["Bike",800]]
salary = input("输入薪水:")
if salary.isdigit():
    salary=int(salary)
    while True:
        for index,item in enumerate(product_list):
            print(index,item)
        user_choice=input("输入选择要买的商品的序号:")
        if user_choice.isdigit():
            user_choice=int(user_choice)
            if user_choice>=0 and user_choice<=len(product_list):
                p_item=product_list[user_choice]
                if p_item[1]<salary:
                    salary-=p_item[1]
                    shopping_list.append(p_item)
                    print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m"%(p_item[0],salary))
                else:
                    print("\033[41;1m你的余额只剩%s啦,不够啦\033[0m"%salary)
            else:
                print("选择的商品不存在")
        else:
            print("结束")
            #输出数据
            print("------------shopping cart----------")
            for i in shopping_list:
                print(i)
            print("你的余额:",salary)
            exit()
else:
    print("结束")

八、三级菜单

要求: 

  1. 打印省、市、县三级菜单
  2. 可返回上一级
  3. 可随时退出程序
menu = {
    '北京':{
        '海淀':{
            '五道口':{
                'soho':{},
                '网易':{},
                'google':{}
            },
            '中关村':{
                '爱奇艺':{},
                '汽车之家':{},
                'youku':{},
            },
            '上地':{
                '百度':{},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '北航':{},
            },
            '天通苑':{},
            '回龙观':{},
        },
        '朝阳':{},
        '东城':{},
    },
    '上海':{
        '闵行':{
            "人民广场":{
                '炸鸡店':{}
            }
        },
        '闸北':{
            '火车战':{
                '携程':{}
            }
        },
        '浦东':{},
    },
    '山东':{},
}while True:
    for i in menu:
        print(i)
    choice1=input("你的选择1>>:")
    if choice1 in menu:
        while True:
            for i2 in menu[choice1]:
                print("\t",i2)
            choice2=input("你的选择2>>:")
            if choice2 in menu[choice1]:
                while True:
                    for i3 in menu[choice1][choice2]:
                        print("\t\t",i3)
                    choice3=input("你的选择3>>:")
                    if choice3 in menu[choice1][choice2]:
                        for i4 in menu[choice1][choice2][choice3]:
                            print("\t\t",i4)
                        choice4=input("最后一层,按b返回")
                        if choice4=='b':
                            pass
                    if choice3=="b":
                        break
            if choice2=="b":
                break

猜你喜欢

转载自www.cnblogs.com/yanqiyyy/p/10783448.html