zoukankan      html  css  js  c++  java
  • day005-python基础数据类型之课后作业讲解

    1、元素的分类

    需求:有如下集合[11,22,33,44,55,66,77,88,99,90……],将所有大于66的值保存在字典的第一个key中,将小于66的值保存在第二个key的值中
    代码实现:
     1 #定义一个list列表
     2 li = [11,22,33,44,55,66,77,88,99,90]
     3 #定义空字典
     4 dic = {"k1": [], "k2": []}
     5 for i in li:
     6     if i <= 66:
     7         dic['k1'].append(i)
     8     else:
     9         dic['k2'].append(i)
    10 print(dic)            
    View Code

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

    代码实现:

    li = ["aleb", "aric", "Alex", "Tony", "rain"]
    #tu = ("alec", "aric", "Alex", "Tony", "rain")    元组也适用以下方法
    for i in li:
        #i表示每一个元素
        new_i = i.strip()
        #if 判断的顺序,从前到后;or,自动执行成功就行;and
        #先执行括号内的,再执行括号外的
        if (new_i.startswith('a') or new_i.startswith('A')) and new_i.endswith('c'):
            print(i)
    #字典
    dic = {"k1": "alex", "k2": "arlc", "k3": "Alex", "k4": "Tony"}
    for j in dic.values():
        new_j = j.strip()
        if (new_j.startswith('a') or new_j.startswith('A')) and new_j.endswith('c'):
            print(j)
    View Code

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

    代码实现:

    li = ["手机", "电脑", "鼠标垫", "游艇"]
    
    #首先循环商品
    for i, j in enumerate(li):
        print(i + 1, j)
    #用户输入
    num = input("请您输入商品编号: ")
    #获取索引
    num = int(num)
    #获取列表的长度
    len_li = len(li)
    if num > 0 and num <= len_li:
        goods = li[num - 1]
        print(goods)
    else:
        print("此商品不存在!")    
    View Code

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

    代码实现:

    dic = {
        "广东": {
            "广州": ["天河区", "越秀区", "海珠区"],
            "东莞":["长安", "凤岗", "黄江"],
        },
        "河北": {
            "石家庄": ["鹿泉", "元氏", "鹰城"],
            "邯郸": ["永年", "涉县", "磁县"], 
        }
    }
    
    #循环输出所有的省
    for x in dic:
        print(x)
    
    #用户输入省份
    i1 = input("请输入省份: ")
    a = dic[i1]
    #循环输出所有的市
    for j in a:
        print(j)
    #用户输入城市 
    i2 = input("请输入城市: ")
    b = dic[i1][i2]
    #循环,将所有的数据打印出来
    for z in b:
        print(z)   
    View Code

    5、购物车

    功能需求:
    要求用户输入总资产,例如:2000
    显示商品列表,让用户根据序号选择商品,加入购物车
    购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功
    附加:可充值、某商品移除购物车
    代码实现方法一:使用列表方式储存购买数量
    #定义一个总资产
    asset_all = 0
    #定义一个购物车列表
    car_list = []
    
    num = input('请输入您的总资产: ')
    asset_all = int(num)
    #所有的商品列表
    goods = [
        {"name": "电脑", "price": 4999},
        {"name": "自行车", "price": 999},
        {"name": "耳机", "price": 129},
        {"name": "iphone", "price": 10999},
    
    ]
    for i in goods:
        #i,每一个列表的元素,字典
        print(i['name'], i['price'])
    
    while True:
        g = input('请输入你选择的商品(y结算): ')
        if g.lower() == 'y':
            break
        for j in goods:
            if j['name'] == g:
            # j = {"name": "电脑", "price": 4999}
                car_list.append(j)
    #结算            
    print(car_list)
    #定义一个购买总价
    all_price = 0        
    for item in car_list:
        p = item['price']
        all_price = all_price + p
    
    #总资产与购买总价比较
    if all_price > asset_all:
        print('您当前的余额不足')
    else:
        print('购买成功')        
    View Code

    代码实现方法二:使用字典方式储存购买数量

    #定义一个总资产
    asset_all = 0
    
    num = input('请输入您的总资产: ')
    asset_all = int(num)
    #所有的商品列表
    goods = [
        {"name": "电脑", "price": 4999},
        {"name": "自行车", "price": 999},
        {"name": "耳机", "price": 129},
        {"name": "iphone", "price": 10999},
    
    ]
    for i in goods:
        #i,每一个列表的元素,字典
        print(i['name'], i['price'])
    
    #定义一个购物车字典
    car_dict = {}
    """ 
    car_dict = {
        "电脑": {'price':单价, num:12}
    } """
    
    while True:
        g = input('请输入你选择的商品(Y/y结算): ')
        #循环所有的商品,查找需要的商品
        if g.lower() == 'y':
            break
        for item in goods:
            if item['name'] == g:
                # j = {"name": "电脑", "price": 4999}
                name = item['name']
                #判断购物车是否已经有该商品, 如果有,num + 1
                if name in car_dict.keys():
                    car_dict[name]['num'] = car_dict[name]['num'] + 1
                else:
                    car_dict[name] = {"num": 1, "single_price": item['price']}            
               
    #结算,显示购物车里的所有商品
    print(car_dict)
    #定义一个购买总价
    all_price = 0
    for k, v in car_dict.items():
        #单价
        n = v['single_price']
        #数量
        m = v['num']
        #计算每类商品的和
        all_sum = m * n
        #购买总价
        all_price = all_price + all_sum   
    #总资产与购买总价比较
    if all_price > asset_all:
        print('您当前的余额不足')
    else:
        print('购买成功')
    View Code
  • 相关阅读:
    Java WebService入门实例
    Maven是什么
    for 循环
    2.4 DevOps工程师的故事:构建运行时的严谨性
    2.3.3 构建微服务的入口:Spring Boot控制器
    2.3.2 引导Spring Boot应用程序:编写引导类
    2.1.3 互相交流:定义服务接口
    第2章 使用Spring Boot构建微服务
    第1章 欢迎来到Cloud和Spring
    第一次在博客园开博
  • 原文地址:https://www.cnblogs.com/june-L/p/11531138.html
Copyright © 2011-2022 走看看