• 数据类型练习题


    一、元素分类

    有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
    即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}

    li = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
    a = {"k1":[], "k2":[]}
    for i in li:
    if i > 66:
    a["k1"].append(i)
    elif i == 66:
    pass
    elif i < 66:
    a["k2"].append(i)
    print(a)=======>{'k1': [77, 88, 99, 90], 'k2': [11, 22, 33, 44, 55]}

    二、查找

    查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。
        li = ["alec", " aric", "Alex", "Tony", "rain"]
        tu = ("alec", " aric", "Alex", "Tony", "rain") 
        dic = {'k1': "alex", 'k2': ' aric',  "k3": "Alex", "k4": "Tony"}
    for i in li:
    b = i.strip()
    if (b.startswith("a") or b.startswith("A")) and b.endswith("c"):
    print(b)

    for i1 in tu:
    b1 = i1.strip()
    if (b1.startswith("a") or b1.startswith("A")) and b1.endswith("c"):
    print(b1)

    for i2 in dic.values():
    b3 = i2.strip()
    if (b3.startswith("a") or b3.startswith("A")) and b3.endswith("c"):
    print(b3)

    三、输出商品列表,用户输入序号,显示用户选中的商品

        商品 li = ["手机", "电脑", '鼠标垫', '游艇']
    li = ["手机", "电脑", '鼠标垫', '游艇']
    for k, v in enumerate(li, 1):===>先排序
    print(k, v)
    inp = int(input("请输入序号:"))====>然后输入号码
    print(li[inp - 1])

    四、购物车

    功能要求:

    • 要求用户输入总资产,例如:2000
    • 显示商品列表,让用户根据序号选择商品,加入购物车
    • 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
    • 附加:可充值、某商品移除购物车
    1
    2
    3
    4
    5
    6
    goods = [
        {"name""电脑""price"1999},
        {"name""鼠标""price"10},
        {"name""游艇""price"20},
        {"name""美女""price"998},
    ]
    goods = [{"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 1},
    {"name": "游艇", "price": 2},
    {"name": "美女", "price": 998}]
    account = []
    sample = 2000
    s = 0

    #显示商品列表代码:
    print("序号 ","name ","price ")
    for i,v in enumerate(goods, 0):
    print(i," ", v["name"], " ", v["price"], " ")

    '''

    序号 name price
    0 电脑 1999
    1 鼠标 1
    2 游艇 2
    3 美女 998

    '''


    item = []
    sample = 2000
    s = 0
    index = []

    while True:
    inp = int(input("请输入序号").strip())
    if inp < len(goods) and inp >= 0:
    item1 = goods[inp]
    money = item1["price"]
    item.append(item1)
    index.append(inp)
    print(goods[inp]["name"], goods[inp]["price"], "已加入购物车")
    s = s + money
    if s <= sample:
    print(goods[inp]["name"], goods[inp]["price"], "购买成功")
    else:
    a = s - sample
    b = "差" + str(a) + "元"
    print("余额不足", b)
    while True:
    inp1 = input("请输入充值/移除:")
    if inp1 == "充值":
    inp2 = input("充值金额:")
    sample += inp2
    if sample >= s:
    print("购买成功")
    break
    else:
    c = "差" + str(s - sample) + "元"
    print("余额不足", c)

    elif inp1 == "移除":
    while True:
    inp3 = int(input("移除商品序号:"))
    if inp3 in index:
    item.remove(goods[int(inp3)])
    index.remove(inp3)
    s = s - goods[int(inp3)]["price"]
    if s > sample:
    d = "差" +str(s - sample) +"元"
    print("余额不足", d)

    else:
    print("购买成功")
    break

    else:
    print("移除错误")
    else:
    print("无此商品")



     五、用户交互,显示省市县三级联动的选择

    dic = {"河北":
    {"石家庄": ["鹿泉", "藁城", "元氏"],"邯郸": ["永年", "涉县", "磁县"]},
    "河南": {},
    "山西": {}
    }
    分析:
    首先需要显示省份,
    比如显示石家庄 dic["河北"],
    比如显示"鹿泉", "藁城", "元氏" dic["河北"]["石家庄"]
               
    代码如下:

    for a in dic.keys():
    print(a)======>显示省份
    province = input("请输入省份:")===>选择输入省份
    for b in dic[province].keys():
    print(b)
    city = input("请输入城市")
    for c in dic[province][city]:
    print(c)











  • 相关阅读:
    Docker之设置加速器
    Nginx之美多商城前台部署
    Nginx之负载均衡
    Nginx之反向代理
    美多商城后台MIS系统部署之Nginx配置
    Nginx之server和location配置使用
    题解 P4721 【【模板】分治 FFT】
    题解 P3338 【[ZJOI2014]力】
    题解 CHSEQ22 【Chef and Favourite Sequence】
    题解 CF813F 【Bipartite Checking】
  • 原文地址:https://www.cnblogs.com/wode110/p/14290854.html
Copyright © 2020-2023  润新知