zoukankan      html  css  js  c++  java
  • python基本数据类型练习

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    list1 = [11,22,33,44,55,66,77,88,99,90]
    dic1 = {
         'k1':[],
         'k2':[]
    }
    for in  list1:
        if l > 66:
            dic1['k1'].append(l)
        else:
            dic1['k2'].append(l)
    print(dic1)

     二、查找
    1、 查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素

    1
    2
    3
    4
    5
    6
    7
    8
    li = ["alc"" aric ""Aex""Tny""rain"]
    list1 =[]
    for in li:
        #使用strip方法确定能寻找到所有元素,startwith,endwith按条件进行查找
        if l.strip().startswith('a'or 'A') and l.strip().endswith('c'):
            #print(l.strip())
            list1.append(l.strip())
    print(list1)

    2、元组

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    tu = ("alc"" aric""Alx""Tny""rain")
    #找出的元素放到一个新列表中,因为元组中不能增加元素
    list2 =[]
    for in tu:
        #使用strip方法确定能寻找到所有元素,startwith,endwith按条件进行查找
        #if 判断遇到or和and是需要注意执行成功时的判断
        if l.strip().startswith('a'or 'A') and l.strip().endswith('c'):
            #print(l.strip())
            list2.append(l.strip())
    print(list2)

    3、字典

    1
    2
    3
    4
    5
    6
    7
    8
    dic = {'k1'"alx"'k2'' aric',  "k3""Alx""k4""Tny","k5":" Anc "}
    #定义一个空字典
    dic1 = {}
    for k,v in dic.items():
        if (v.strip().startswith('a') or v.strip().startswith('A')) and v.strip().endswith('c'):
            print(v)
            dic1[k] =v
    print(dic1)

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #  商品
    li = ["手机""电脑"'鼠标垫''键盘']
    for num,v in enumerate(li,1):
         print(num,v)
    choice = int(input("请选择商品:"))
    choice1=choice-1
    if choice1>=0 and choice1<=len(li)-1:
        print(li[choice1])
    else:
         print("商品不存在")

    四、购物车
    # 功能要求:
    #     要求用户输入总资产,例如:2000
    #     显示商品列表,让用户根据序号选择商品,加入购物车
    #     购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
    #     附加:可充值、某商品移除购物车
    方法一:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    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
    goods = [
        {"product""电脑""price": 1999},
        {"product""鼠标""price": 10},
        {"product""iphone""price": 5000},
        {"product""kindle""price": 998},
    ]
    #已经买到的商品
    list_buy = []
    #输入总资产
    all_money = 0
    all_money = int(input("请输入总资产:"))
    #输出所有的产品
    for key,i in enumerate(goods,1):
        print(i['product'],i['price'])
    #当条件成立时,在购买环节循环
    while True:
        #选择需要买的商品
        choice = input("请选择商品(y/Y进行结算购买):")
        #是否进行结算
        if choice.lower() == "y":
            break
     
        #循环所有的商品与选择商品进行对比,如果存在,就添加到list_buy中
        for v in goods:
            if choice == v["product"]:
                list_buy.append(v)
    #输出所有打算购买的商品
    print(list_buy)
    #定义商品总价初始值
    total_price = 0
    for in list_buy:
        #计算所有商品价格
        total_price = total_price+p["price"]
    if total_price>all_money:
        print("你的钱不够,请充值%d元"%(total_price-all_money))
        chongzhi = int(input("输入充值金额:"))
        all_money +=chongzhi
    else:
        print("购买成功")
        print(list_buy)

    方法二:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    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
    goods = [
        {"product""电脑""price": 1999},
        {"product""鼠标""price": 10},
        {"product""iphone""price": 5000},
        {"product""kindle""price": 998},
    ]
    salary = int(input("请输入工资:"))
    #dic_shop_cart  = {"product":{"price":0,"num":0}}
    dic_shop_cart = {}
    #循环输出所有产品
    for in goods:
        print(p['product'],p['price'])
    while True:
        choice = input("请选择购买的商品(y/Y进行结算):")
        if choice.lower() == 'y':
            break
        #循环所有商品
        for item in goods:
            #判断选择的商品是否在所有商品中
            if item["product"] == choice:
                #如果存在,就把商品赋值给product
                product = item["product"]
                #如果商品在字典dic_shop_cart中,字典中num就加1
                if product in dic_shop_cart.keys():
                    dic_shop_cart[product]["num"] = dic_shop_cart[product]["num"] + 1
                    #如果不在,就第一次添加到字典中
                else:
                    dic_shop_cart[product] = {"num":1,"single_price":item["price"]}
        print(dic_shop_cart)
    sum_price = 0
    for k,v in dic_shop_cart.items():
    #    print(k,v)
        t_price = v["single_price"]*v["num"]
        print("购买%s的数量为%s:总价为%d"%(k,v["num"],t_price))
        sum_price=sum_price+t_price
    print("所有商品总价为:%s"%sum_price)
    if sum_price>salary:
        print("你的钱不够,哈哈哈。。。,别买了吧")
    else:
        print("购买成功,有钱人啊。。。")

     输出结果:


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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    dic = {
        "河北": {
            "石家庄": ["鹿泉""藁城""元氏"],
            "邯郸": ["永年""涉县""磁县"],
        },
        "北京": {
            "大兴": ["黄村""清源""天宫院"],
            "海淀": ["中关村""西二旗""五道口"],
        },
        "安徽": {
            "合肥": ["庐阳""肥西""滨湖"],
            "安庆": ["桐城""宜秀区""岳西"],
        }
    }
    for in dic:
        print(p)
    p1 = input("请输入省份:")
    if p1 in dic.keys():
        for in dic[p1]:
             print(s)
        s1 = input("请输入市区:")
        if s1 in dic[p1].keys():
            for in  dic[p1][s1]:
                print(q)
        else:
            print("市区还没有录入")
    else:
        print("省份还没有录入")

     执行结果:

  • 相关阅读:
    python--binascii--二进制和ASCII编码的二进制的转换
    python--you-get视频下载
    python--AES加密
    nodejs的简单爬虫
    golang学习之接口型函数
    golang学习之defer
    golang学习之slice基本操作
    微信小程序初体验
    vuex构建笔记本应用学习
    2016年终总结
  • 原文地址:https://www.cnblogs.com/chenshengqun/p/9071228.html
Copyright © 2011-2022 走看看