博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基本数据类型编程练习一
阅读量:5305 次
发布时间:2019-06-14

本文共 5317 字,大约阅读时间需要 17 分钟。

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 += 1102 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 # 将余额状态赋值给salary115 break116 else:117 salary = int(salary)118 continue119 120 else:121 print('谢谢惠顾')122 break123 print('你的余额:', salary)124 print('购买的商品:', shoping_car)125 126 '''127 ***************执行结果***************128 输入您的总资产:2000129 1 >>> 电脑130 2 >>> 鼠标131 3 >>> 游艇132 4 >>> 美女133 选择商品序列号[按q 退出]:1134 选择商品序列号[按q 退出]:1135 你的余额不足,请充值136 输入充值额度[按q 退出]:1000137 选择商品序列号[按q 退出]:4138 选择商品序列号[按q 退出]:1139 你的余额不足,请充值140 输入充值额度[按q 退出]:q141 你的余额: q142 购买的商品: ['电脑', '美女']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 = dic184 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: continue191 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

 

转载于:https://www.cnblogs.com/JerryZao/p/8591829.html

你可能感兴趣的文章
创新课程管理系统数据库设计心得
查看>>
Hallo wolrd!
查看>>
16下学期进度条2
查看>>
Could not resolve view with name '***' in servlet with name 'dispatcher'
查看>>
Chapter 3 Phenomenon——12
查看>>
和小哥哥一起刷洛谷(1)
查看>>
jquery对id中含有特殊字符的转义处理
查看>>
遇麻烦,Win7+Ubuntu12.10+Archlinux12.10 +grub
查看>>
SqlBulkCopy大批量导入数据
查看>>
pandas 修改指定列中所有内容
查看>>
「 Luogu P2285 」打鼹鼠
查看>>
lua语言入门之Sublime Text设置lua的Build System
查看>>
vue.js基础
查看>>
电脑的自带图标的显示
查看>>
[转载] redis 的两种持久化方式及原理
查看>>
C++ 删除字符串的两种实现方式
查看>>
ORA-01502: 索引'P_ABCD.PK_WEB_BASE'或这类索引的分区处于不可用状态
查看>>
Java抽象类和接口的比较
查看>>
开发进度一
查看>>
MyBaits学习
查看>>