Learn_Day4
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Learn_Day4相关的知识,希望对你有一定的参考价值。
作业讲解
# /usr/local/bin/python
# -*- coding: utf-8 -*-
# Author:Ren Hang
"""
一、元素分类
有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
即: {‘k1‘: 大于66的所有值, ‘k2‘: 小于66的所有值}
"""
# 方法1
li = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
l1 = []
l2 = []
for i in li:
if i < 66:
l1.append(i)
elif i > 66:
l2.append(i)
temp = {"k1": l1, "k2": l2}
print(temp)
# {‘k1‘: [11, 22, 33, 44, 55], ‘k2‘: [77, 88, 99, 90]}
# 方法2
li = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
dic = {
‘k1‘: [],
‘k2‘: []
}
for i in li:
if i < 66:
dic["k1"].append(i)
elif i > 66:
dic["k2"].append(i)
print(dic)
# {‘k1‘: [11, 22, 33, 44, 55], ‘k2‘: [77, 88, 99, 90]}
"""
二、查找
查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。
li = ["alec", " aric", "Alex", "Tony", "rain"]
tu = ("alec", " aric", "Alex", "Tony", "rain")
dic = {‘k1‘: "alex", ‘k2‘: ‘ aric‘, "k3": "Alex", "k4": "Tony"}
"""
li = ["alec", " aric", "Alex", "Tony", "rain"]
tu = ("alec", " aric", "Alex", "Tony", "rain")
dic = {‘k1‘: "alex", ‘k2‘: ‘ aric‘, "k3": "Alex", "k4": "Tony"}
# 方法1
# 循环li
for i in li:
new_i = i.strip() # 去除空格
if (new_i.startswith("a") or new_i.startswith("A")) and new_i.endswith("c"):
print("a1", i)
# 循环tu
for i in li:
new_i = i.strip() # 去除空格
if (new_i.startswith("a") or new_i.startswith("A")) and new_i.endswith("c"):
print("b2", i)
# 循环dic
for i in dic.values():
new_i = i.strip()
if (new_i.startswith("a") or new_i.startswith("A")) and new_i.endswith("c"):
print("c3", i)
# 方法2
# 循环li
for i in li:
b = i.strip() # 去除空格
# print(b)
if b.startswith("a") and b.endswith("c"): # 判断以a开头以c结束的元素
print("1", b) # 输出元素
elif b.startswith("A") and b.endswith("c"): # 判断以A开头以c结束的元素
print("2", b) # 输出元素
# 循环tu
for i in tu:
b = i.strip() # 去除空格
# print(b)
if b.startswith("a") and b.endswith("c"): # 判断以a开头以c结束的元素
print("3", b) # 输出元素
elif b.startswith("A") and b.endswith("c"): # 判断以A开头以c结束的元素
print("4", b) # 输出元素
# 循环dic
for k, v in dic.items():
b1 = k.strip() # 去除空格
b2 = v.strip() # 去除空格
if b1.startswith("a") and b1.endswith("c"): # 判断以a开头以c结束的元素
print("5", b1) # 输出元素
elif b1.startswith("A") and b1.endswith("c"): # 判断以A开头以c结束的元素
print("6",b1) # 输出元素
elif b2.startswith("a") and b2.endswith("c"): # 判断以a开头以c结束的元素
print("7", b2) # 输出元素
elif b2.startswith("A") and b2.endswith("c"): # 判断以A开头以c结束的元素
print("8", b2) # 输出元素
# 三、输出商品列表,用户输入序号,显示用户选中的商品
li = ["手机", "电脑", ‘鼠标垫‘, ‘游艇‘]
# 方法1
while True:
for i, j in enumerate(li):
print(i+1, j)
num = input("num:")
if num.isdigit():
num = int(num)
if 0 < num < len(li):
print(li[num-1])
break
else:
print("输入错误!请输入商品对应的编号:")
else:
print("输入错误!请输入商品对应的编号:")
"""
四、购物车
功能要求:
1.要求用户输入总资产,例如:2000
2.显示商品列表,让用户根据序号选择商品,加入购物车
3.购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
4.附加:可充值、某商品移除购物车
"""
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
# 方法2
# asset_all 总资产
asset_all = 0
# car_dict 购物车
car_dict = {}
# all_pric 已经购买所以的商品总价
all_price = 0
# all_sum 已经购买单个的商品总价
all_sum = 0
i1 = input("请输入总资产:")
asset_all = int(i1)
for i in goods:
print(i["name"], i["price"])
while True:
i2 = input("【Y/y:结算】输入要购买的商品名称:")
if i2.lower() == "y":
break
for item in goods:
if item["name"] == i2:
name = item["name"]
if name in car_dict.keys():
car_dict[name]["num"] += 1
else:
car_dict[name] = {"num": 1, "single_price": item["price"]}
print(car_dict)
for k, v in car_dict.items():
all_sum = v["num"] * v["single_price"]
all_price += all_sum
if all_price > asset_all:
print("余额不足!")
else:
print("购买成功!”)
"""
五、用户交互,显示省市县三级联动的选择
"""
dic = {
"北京": {
"朝阳区": ["123", "456"],
"崇文区": ["789", "101"]
},
"辽宁省": {
"沈阳市": ["铁西区", "沈河区"],
"锦州市": ["古塔区", "黑山县"],
},
}
# 方法1
# 循环输出所以的省
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)
以上是关于Learn_Day4的主要内容,如果未能解决你的问题,请参考以下文章