# 一、元素分类
# 有如下值集合[11, 22, 33, 44, 55, 66, 77, 88, 99, 90...],将所有大于
# 66
# 的值保存至字典的第一个key中,将小于
# 66
# 的值保存至第二个key的值中。
# 即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
# a = [11,22,33,44,55,66,77,88,99]
# b = []
# c = []
# dict = {"k1":b,"k2":c}
# for i in a:
# if i >66:
# b.append(i)
# if i <66:
# c.append(i)
# print(dict)
# 二、查找
# 查找列表中元素,移除每个元素的空格,并查找以a或A开头并且以c结尾的所有元素。
# li = ["alec", " aric", "Alex", "Tony", "rain"]
# tu = ("alec", " aric", "Alex", "Tony", "rain")
# dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"}
# for i in li:
# i = i.strip()
# if (i.startswith('A') or i.startswith('a')) and i.endswith('c') :
# print(i)
# for i in tu:
# i = i.strip()
# if (i.startswith('A') or i.startswith('a')) and i.endswith('c') :
# print(i)
# for i in dic.values():
# i = i.strip()
# if (i.startswith('A') or i.startswith('a')) and i.endswith('c'):
# print(i)
# 三、输出商品列表,用户输入序号,显示用户选中的商品
# 商品
# li = ["手机", "电脑", '鼠标垫', '游艇']
# while True:
# choice=int(input('请输入序号:'))
# print(li[choice])
# 四、购物车
# 功能要求:
# 要求用户输入总资产,例如:2000
# 显示商品列表,让用户根据序号选择商品,加入购物车
# 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
# 附加:可充值、某商品移除购物车
# goods = [
# {"name": "电脑", "price": 1999},
# {"name": "鼠标", "price": 10},
# {"name": "游艇", "price": 20},
# {"name": "美女", "price": 998},
# ]
# shopping_cart=[]
# flag=True
# sum=0
# while flag:
# money=input("请输入您的账户金额:").strip()
# if money.isdigit():
# money=int(money)
# print("商品列表:")
# while flag:
# for i in range(len(goods)):
# print(i+1,goods[i]["name"],goods[i]["price"])
# choice=input("请选择您需要购买的商品或退出(q):").strip()
# num=0
# if choice.isdigit():
# choice=int(choice)
# choice=choice-1
# if choice >=0 and choice <len(goods):
# if money > goods[choice]['price']:
# product=goods[choice]
# if product in shopping_cart:
# num+=1
# else:
# shopping_cart.append(product)
# money = money - goods[choice ]["price"]
# sum+=goods[choice ]["price"]
# print("购买成功,您购买了"+goods[choice]['name'],'您还剩'+str(money)+'元钱')
# elif money<goods[choice]['price']:
# print("对不起,您的账户余额不足!")
# flag=False
# else:
# print('商品不存在')
# continue
# elif choice.lower()=='q':
# print('---购物清单---:')
# for k,v in enumerate(shopping_cart,1):
# print(k,v['name'],v['price'],num)
# print('共消费'+str(sum)+'元'+'\n您的余额是'+str(money)+'元'+'\n---结束购物---')
# flag=False
# else:
# print('请输入数字或q')
# continue
# else:
# print('请输入数字')
# continue
# 选做题:用户交互,显示省市县三级联动的选择
dic = {
"河北": {
"石家庄": ["鹿泉", "藁城", "元氏"],
"邯郸": ["永年", "涉县", "磁县"],
},
"河南": {
'a':[1,2,3],
'c':[4,5,6]
},
"山西": {
'd':[7,8,9],
'e':[10,11,12]
}
}
# 第一种
# flag=True
# while flag:
# for i in dic:
# print(i)
# choice1=input('>>:').strip()
# if choice1.upper()=='Q':
# break
# next1=dic[choice1]
# for j in next1:
# print(j)
# while flag:
# choice2=input('>>:').strip()
# if choice2.upper()=='Q':
# break
# if choice2.upper()=='B':
# flag=False
# break
# next2=next1[choice2]
# for z in next2:
# print(z)
# choice3 = input('>>:').strip()
# if choice3.upper()=='Q':
# flag=False
# break
# if choice3.upper()=='B':
# continue
exit_flag = False
while not exit_flag:
for key in dic:
print(key)
choice = input(">:").strip()
if len(choice) == 0 : continue
if choice == 'q':
exit_flag = True
continue
if choice in dic: #省存在,进入此省下一级
while not exit_flag:
next_layer = dic[choice]
for key2 in next_layer:
print(key2)
choice2 = input(">>:").strip()
if len(choice2) == 0: continue
if choice2 == 'b': break
if choice2 == 'q':
exit_flag = True
continue
if choice2 in next_layer: #再进入下一层
while not exit_flag:
next_layer2 = next_layer[choice2]
for key3 in next_layer2:
print(key3)
choice3 = input(">>>:").strip()
if len(choice3) == 0: continue
if choice3 == 'b': break
if choice3 == 'q':
exit_flag = True
continue
if choice3 in next_layer2:
while not exit_flag:
next_layer3 = next_layer2[choice3]
for key4 in next_layer3:
print(key4)
choice4 = input(">>>>:").strip()
if choice4 == 'b':break
if choice4 == 'q':
exit_flag = True
continue
# 第二种
# current_layer=dic
# layers=[dic]
# while True:
# for i in current_layer:
# print(i)
# choice=input('>>>:').strip()#用户输入选项
# #判断输入是否有效
# if choice in current_layer:
# layers.append(current_layer)#把当前层菜单添加到全部层,用于返回上一级使用
# current_layer=current_layer[choice]#把当前层替换新的
# elif choice.upper()=='B':
# current_layer=layers[-1]
# layers.pop()
# elif choice.upper()=='Q':
# break