python基础知识总结实例(简单的小程序)

本文通过一个购物车项目的实例,介绍了Python的基础知识,包括enumerate函数的使用、字符串数字检查、break和continue语句在循环中的应用,以及字典嵌套操作,特别是查找地名的方法。
摘要由CSDN通过智能技术生成

购物车项目


product_list=[
    ('Mac',9000),
    ('Book',90),
    ('bicyle',1000),
    ('comeputer',5000),
]
for i,v in enumerate(product_list,1):
    print(i,">>>",v)
shopping_car = []
saving = input("请输入您的银行卡余额:")
if saving.isdigit():
    saving=int(saving)
    while True:
        for i,v in enumerate(product_list,1):  #enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
            print(i,'>>>',v)
        choice=input('选择购买商品的编号去,q【退出购物】')
        if choice.isdigit():
            choice=int(choice)
            if choice > 0 and choice <= len(product_list):
                p_item = product_list[choice-1] # 去出商品的名称和价格
                if p_item[1] < saving:
                    saving -= p_item[1] #求取用户余额
                    shopping_car.append(p_item)  # 将商品加入购物车
                else:
                    print('余额不足,还剩%s' %saving)
                for i,v in enumerate(p_item):

                    print('商品%s'%v,end="")
                print('您当前的余额是%s'%saving)
            else:
                print('当前商品编码不存在')

        elif choice == "q":
            print("您购买了如下产品,您的余额是%s元,欢迎下次光临"%saving)
            for i in shopping_car:
                print(i)
            break
        else:
            print('What do you want!')

使用的函数简单介绍

enumerate() # 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
enumerate(sequence, [start=0])

参数
sequence – 一个序列、迭代器或其他支持迭代对象。
start – 下标起始位置。 # 可自行添加下标起始位置

返回值
返回 enumerate(枚举) 对象。

isdigit() # 方法检测字符串是否只由数字组成。

如果字符串只包含数字则返回 True 否则返回 False。

break 关键字
break语句用来终止循环语句,即循环条件没有False条件或者序列还没被完全递归完,也会停止执行循环语句。

break语句用在while和for循环中。

如果您使用嵌套循环,break语句将停止执行最深层的循环,并开始执行下一行代码。

continue 关键字
Python continue 语句跳出本次循环,而break跳出整个循环。

continue 语句用来告诉Python跳过当前循环的剩余语句,然后继续进行下一轮循环。

continue语句用在while和for循环中。

python字典嵌套之找地名

menu = {
    "甘肃": {"兰州": {
        "永登": {"秦川": "",
               "城管镇": "",
               "红城镇": "",
               "和桥镇": "",
               "苦水镇": "",
               "中川镇": "",
               },
        "七里河": {"西园街道": "",
                "西湖街道": "",
                "建兰路街道": "",
                "敦煌路街道": "",
                "西站街道": "",
                "阿干镇": "",
                },

    },
        "白银": {
            "白银区": {"人民路街道": "",
                    "公园路街道": "",
                    "工农路街道": "",
                    "水川镇": "",
                    },
            "平川区": {"长征街道": "",
                    "电力路街道": "",
                    "兴平路街道": "",
                    "红会路街道": "", },
            "会宁县": {"会师镇": "",
                    "太平店镇": "",
                    "侯家川镇": "",
                    "柴家门镇": "", },
            "景泰县": {"一条山镇": "",
                    "胡阳镇": "",
                    "红水镇": "",
                    "五佛乡": "",
                    }
        }
    },
    "陕西": {"西安": {
        "永登": {"秦川": "",
               "城管镇": "",
               "红城镇": "",
               "和桥镇": "",
               "苦水镇": "",
               "中川镇": "",
               },
        "七里河": {"西园街道": "",
                "西湖街道": "",
                "建兰路街道": "",
                "敦煌路街道": "",
                "西站街道": "",
                "阿干镇": "",
                },

    },
        "宝鸡": {
            "白银区": {"人民路街道": "",
                    "公园路街道": "",
                    "工农路街道": "",
                    "水川镇": "",
                    },
            "平川区": {"长征街道": "",
                    "电力路街道": "",
                    "兴平路街道": "",
                    "红会路街道": "", },
            "会宁县": {"会师镇": "",
                    "太平店镇": "",
                    "侯家川镇": "",
                    "柴家门镇": "", },
            "景泰县": {"一条山镇": "",
                    "胡阳镇": "",
                    "红水镇": "",
                    "五佛乡": "",
                    }
        }
    }

}

current_layer = menu
parent_layer = []
choices = []
con = True
while con:
    for key in current_layer:
        print(key)
        

    choice = input("Enter the layer of your choice:>>>")
    print("Enter b return to the next level")
    if len(choice) == 0: continue
    if choice in current_layer:
        # print(list(current_layer)[-2])
        # print(current_layer)
        print(current_layer[choice])
        parent_layer.append(current_layer)
        current_layer = current_layer[choice]
        choices.append(choice)
        if current_layer == "":
            print("你当前所在的位置是{}".format(choices))
            break
            con = False

    elif choice == "b":
        current_layer = parent_layer.pop()
    else:
        print(" Please select the right layer ")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值