基本数据类型编程练习一

  1 '''
  2 一、元素分类
  3 
  4 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
  5 即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
  6 '''
  7 li = [11,22,33,44,55,66,77,88,99,90]
  8 k1 = ''
  9 k2 = ''
 10 li = {}
 11 for i in li:
 12     if i > 66:
 13         k1 = k1 + str(i)
 14     if i < 66:
 15         k2 = k2 + str(i)
 16 li.update(k1 = '大于66的所有值',k2 = '小于66的所有值')
 17 print(li)
 18 print(k1,k2)
 19 
 20 '''
 21 二、查找
 22 查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。
 23     li = ["alec", " aric", "Alex", "Tony", "rain"]
 24     tu = ("alec", " aric", "Alex", "Tony", "rain") 
 25     dic = {'k1': "alex", 'k2': ' aric',  "k3": "Alex", "k4": "Tony"}
 26 '''
 27 li = ["alec", " aric", "Alex", "Tony", "rain"]
 28 tu = ("alec", " aric", "Alex", "Tony", "rain")
 29 dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"}
 30 
 31 new_dic = {}
 32 for i in dic.keys():
 33     dic[i] = dic[i].strip()
 34     if (dic[i].startswith('a') or dic[i].startswith('A')) and dic[i].endswith('c'):
 35         new_dic.update(i=dic[i])
 36 print(new_dic)
 37 print(dic)
 38 
 39 先将tuple转换为list,修改后,在转换回去
 40 i = 0
 41 new_tu = list(tu)
 42 new_tu1 = []
 43 while i < len(new_tu):
 44     new_tu[i] = new_tu[i].strip()
 45     if  (new_tu[i].startswith("a") or new_tu[i].startswith("A")) and new_tu[i].endswith("c"):
 46         new_tu1.append(new_tu[i])
 47     i += 1
 48 print(tuple(new_tu))
 49 print(tuple(new_tu1))
 50 
 51 
 52 i = 0
 53 new_li = []
 54 # print(type(new_li))
 55 while i < len(li):
 56     li[i] = li[i].strip()
 57     if (li[i].startswith("a") or li[i].startswith("A")) and li[i].endswith("c"):
 58         new_li.append(li[i])
 59     i += 1
 60 print(li)
 61 print(new_li)
 62 
 63 '''
 64 三、输出商品列表,用户输入序号,显示用户选中的商品
 65     商品 li = ["手机", "电脑", '鼠标垫', '游艇']
 66 '''
 67 li = ["手机", "电脑", '鼠标垫', '游艇']
 68 for k, v in enumerate(li, 1):
 69     print(k, v)
 70 while True:
 71     s = int(input("输入商品序列号:"))
 72     if s < len(li):
 73         print(li[s])
 74     else:
 75         print("没有该商品序列号")
 76 
 77 '''
 78 四、购物车
 79 goods = [
 80     {"name": "电脑", "price": 1999},
 81     {"name": "鼠标", "price": 10},
 82     {"name": "游艇", "price": 20},
 83     {"name": "美女", "price": 998},
 84 ]
 85 功能要求:
 86     要求用户输入总资产,例如:2000
 87     显示商品列表,让用户根据序号选择商品,加入购物车
 88     购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
 89     附加:可充值、某商品移除购物车
 90 '''
 91 salary = int(input("输入您的总资产:"))
 92 goods = [
 93     {"name": "电脑", "price": 1999},
 94     {"name": "鼠标", "price": 10},
 95     {"name": "游艇", "price": 20},
 96     {"name": "美女", "price": 998},
 97 ]
 98 i = 1
 99 while i <= len(goods):
100     print(i, '>>>', goods[i - 1]['name'])
101     i += 1
102 
103 shoping_car = []
104 while True:
105     num = input('选择商品序列号[按q 退出]:')
106     if num != 'q':
107         num = int(num)
108         if goods[num - 1]['price'] < salary:
109             salary = salary - goods[num - 1]['price']
          a = salary # 保存余额状态
110 shoping_car.append(goods[num - 1]['name']) 111 else: 112 print("你的余额不足,请充值") 113 salary = input('输入充值额度[按q 退出]:') 114 if salary == 'q':
            salary = a # 将余额状态赋值给salary
115 break 116 else: 117 salary = int(salary) 118 continue 119 120 else: 121 print('谢谢惠顾') 122 break 123 print('你的余额:', salary) 124 print('购买的商品:', shoping_car) 125 126 ''' 127 ***************执行结果*************** 128 输入您的总资产:2000 129 1 >>> 电脑 130 2 >>> 鼠标 131 3 >>> 游艇 132 4 >>> 美女 133 选择商品序列号[按q 退出]:1 134 选择商品序列号[按q 退出]:1 135 你的余额不足,请充值 136 输入充值额度[按q 退出]:1000 137 选择商品序列号[按q 退出]:4 138 选择商品序列号[按q 退出]:1 139 你的余额不足,请充值 140 输入充值额度[按q 退出]:q 141 你的余额: q 142 购买的商品: ['电脑', '美女'] 143 ''' 144 145 ''' 146 五、用户交互,显示省市县三级联动的选择 147 dic = { 148 "河北":{ 149 "石家庄" :["鹿泉","藁城","元氏"], 150 "邯郸" : ["永年","涉县","磁县"] 151 }, 152 "河南":{ 153 "郑州":["巩义","登封","新密"], 154 "开封":["金明","鼓楼","通许"] 155 }, 156 "山西":{ 157 "太原":["古交","清徐","阳曲"], 158 "大同":["天镇","阳高","广灵"] 159 } 160 } 161 需求: 162 (1. 运行程序输出第一级菜单 163 (2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单 164 (4. 让用户选择是否要退出 165 (5. 有返回上一级菜单的功能 166 ''' 167 dic = { 168 "河北": { 169 "石家庄": ["鹿泉", "藁城", "元氏"], 170 "邯郸": ["永年", "涉县", "磁县"] 171 }, 172 "河南": { 173 "郑州": ["巩义", "登封", "新密"], 174 "开封": ["金明", "鼓楼", "通许"] 175 }, 176 "山西": { 177 "太原": ["古交", "清徐", "阳曲"], 178 "大同": ["天镇", "阳高", "广灵"] 179 } 180 } 181 print("一级菜单".center(20, '-')) 182 183 current_layer = dic 184 parent_layer = [] 185 186 while True: 187 for k in current_layer: 188 print('>>>>> ', k) 189 menu = input('请选择[按q退出/按b返回上一级]:').strip() 190 if len(menu) == 0: continue 191 if menu != 'q':# 退出 192 if menu in current_layer: 193 if type(current_layer) == dict: #第三层是列表,在往下会出错,所以用if条件判断 194 parent_layer.append(current_layer) 195 current_layer = current_layer[menu] 196 else: 197 print('最后一层了'.center(20,'-')) 198 elif menu == 'b':#返回 199 if parent_layer: 200 current_layer = parent_layer.pop() 201 if current_layer == dic: 202 print('这是第一层了'.center(20,'-')) 203 else: 204 print('重新输入'.center(20,'-')) 205 else: 206 break

 

posted @ 2018-03-17 18:24  JerryZao  阅读(321)  评论(0编辑  收藏  举报