一、元素分类
有如下值集合[11,22,33,44,55,66,77,88,99,90 ...],将所有大于66的值保存至字典的第一个key中,将小于66的值保存至第二个key的值中。
即:{‘k1‘:大于66的所有值,‘k2’小于66的所有值}
二、查找
查找列表中元素,移动空格,并查找以a或A开投并且以c结尾的所有元素。
li = ["alec"," aric","Alex","Tony","rain"]
tu = ("alec"," aric","Alex","Tony","rain")
dic = {"k1":" alec","k2":"aric","k3":"Alex","k4":"Tony"}
三、输出商品列表,用户输入序号,显示用户选中的商品
商品 li = ["手机","电脑","鼠标垫","游艇"]
四、购物车
功能要求:
要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账号余额不足,否则,购买成功。
附加:可充值,某商品移除购物车
一、 #!/usr/bin/env python #-*- coding:utf-8 -*- new1 = [] new2 = [] Ks = {"k1":"None","k2":"None"} Li = [11,22,33,44,55,66,77,88,99,90] for item in Li: if int(item) > 66: new1.append(item) Ks.update({‘k1‘:new1}) else: new2.append(item) Ks.update({‘k2‘: new2}) print(Ks) 二、 #!/usr/bin/env python #-*- coding:utf-8 -*- li = ["alec", " aric", "Alex", "Tony", "rain"] tu = ("alec", " aric", "Alex", "Tony", "rain") dic = {"k1": " alec", "k2": "aric", "k3": "Alex", "k4": "Tony"} Cz1 = input("请输入要查找的元素:") if Cz1 in li: print("True") else: print("False") 三、 #!/usr/bin/env python #-*- coding:utf-8 -*- Li = [‘手机‘,‘电脑‘,‘鼠标垫‘,‘游艇‘] for n,Me in enumerate(Li,1): print(n,"-",Me) try: shop = input("请输入商品序对应列号:\n") print("您选择的商品是:%s"%Li[int(shop)-1]) except Exception: print("没有此商品!!") 四、
原文地址:http://shenyujia.blog.51cto.com/8839395/1910622