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

list1 = [11,22,33,44,55,66,77,88,99,90]
dic1 = {
'k1':[],
'k2':[]
}
for l in list1:
if l > 66:
dic1['k1'].append(l)
else:
dic1['k2'].append(l)
print(dic1)

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

li = ["alc", " aric ", "Aex", "Tny", "rain"]
list1 =[]
for l 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、元组

tu = ("alc", " aric", "Alx", "Tny", "rain")
#找出的元素放到一个新列表中,因为元组中不能增加元素
list2 =[]
for l 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、字典

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)

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

#  商品
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
#     显示商品列表,让用户根据序号选择商品,加入购物车
#     购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
#     附加:可充值、某商品移除购物车
方法一:

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 p 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)

方法二:

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 p 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("购买成功,有钱人啊。。。")

 输出结果:


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

dic = {
"河北": {
"石家庄": ["鹿泉", "藁城", "元氏"],
"邯郸": ["永年", "涉县", "磁县"],
},
"北京": {
"大兴": ["黄村", "清源", "天宫院"],
"海淀": ["中关村", "西二旗", "五道口"],
},
"安徽": {
"合肥": ["庐阳", "肥西", "滨湖"],
"安庆": ["桐城", "宜秀区", "岳西"],
}
}
for p in dic:
print(p)
p1 = input("请输入省份:")
if p1 in dic.keys():
for s in dic[p1]:
print(s)
s1 = input("请输入市区:")
if s1 in dic[p1].keys():
for q in dic[p1][s1]:
print(q)
else:
print("市区还没有录入")
else:
print("省份还没有录入")

 执行结果:

最新文章

  1. spring自定义schema学习
  2. 博客的开端,找对象不再new
  3. LA 3983 Robotruck
  4. HDOJ(HDU) 1862 EXCEL排序(类对象的快排)
  5. web乱码问题
  6. 腾讯地图api接收坐标提交坐标
  7. Centos5搭建vsftpd服务
  8. 谈一谈JDK8的函数式编程 (一)
  9. HX711初步处理记录
  10. git修改已push的commit信息
  11. Beta冲刺三
  12. 怎样将一个Long类型的数据转换成字节数组
  13. js判断数据类型的四种方法
  14. CSS背景渐变支持transition过渡效果
  15. Spring IOC(四)FactoryBean
  16. poj 1068 Parencodings 模拟题
  17. 趣谈unicode,ansi,utf-8,unicode big endian这些编码有什么区别
  18. C#中 如何处理 JSON中的特殊字符
  19. hdu3974 Assign the task【线段树】
  20. Python中进程和线程的总体区别

热门文章

  1. NYOJ-小猴子下落
  2. Windows下安装MySQL-5.7.19
  3. 大数据排序算法:外部排序,bitmap算法;大数据去重算法:hash算法,bitmap算法
  4. 功能强大的Northwoods GoDiagram控件库
  5. Python:easygui的安装、导入、使用、设置
  6. pyodbc简单使用
  7. Sequence Models 笔记(二)
  8. 实现Unity对Dictionary的序列化
  9. VSCode编写C/C++项目
  10. jQuery学习2