实验三 控制语句与组合数据类型应用编程
Posted llldbk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验三 控制语句与组合数据类型应用编程相关的知识,希望对你有一定的参考价值。
task1
实验源码
import random print(\'用列表存储随机整数:\') lst = [random.randint(0, 100) for i in range(5)] print(lst) print(\'\\n用集合存储随机整数:\') s1 = random.randint(0, 100) for j in range(5) print(s1) print(\'\\n用集合存储随机整数:\') s2 = set() while len(s2) < 5: s2.add(random.randint(0,100)) print(s2)
运行截图
问题1:[0,100] , 可以
问题2:[0,1,2,3,4] , 不包括
[1,2,3,4] , 不包括
问题3:不一定,遍历生成的随机5个数可能重复,而集合中的元素不能重复。所以len
(s1)可能小于5。
问题4:是
task2_1
实验源码
#列表遍历 lst = [55, 92, 88, 79, 96] #1.while+索引 i = 0 while i < len(lst): print(lst[i], end=\' \') i += 1 print() #2.for+索引 for i in range(len(lst)): print(lst[i], end=\' \') print() #3.for...in for i in lst: print(i, end=\' \')
运行截图
task2_2
实验源码
#字典遍历 book_info = \'isbn\': \'978-7-5356-8297-0\', \'书名\': \'白鲸记\', \'作者\': \'克里斯多夫.夏布特\', \'译者\': \'高文婧\', \'出版社\': \'湖南美术出版社\', \'售价\':\'82\' #1.key,value for key, value in book_info.items(): print(f\'key:value\') #2.item for item in book_info.items(): print(f\'item[0]:item[1]\') #3.值value for value in book_info.values(): print(value, end=\' \') print() #值key for key in book_info.keys(): print(key,end=\' \')
运行截图
task3
实验源码
x = \'\'\' The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren\'t special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you\'re Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it\'s a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let\'s do more of those! \'\'\'
x = x.lower() t = [] for i in range(97, 97+26): c = x.count(chr(i)) t.append([c, chr(i)]) t = sorted(t, reverse=True) for j in t: print(f\'j[1]:j[0]\')
运行截图
task4
实验源码
d = 8326: \'地信类\', 8329: \'计算机类\', 8330: \'气科类\', 8336: \'防灾工程\', 8345: \'海洋科学\', 8382: \'气象工程\' print(\':-^50\'.format(\'专业代号信息\')) for i in d.items(): print(f\'i[0]: i[1]\') print(\':-^50\'.format(\'学生专业查询\')) while True: x = input(\'请输入学号:\') if x == \'#\': print(\'查询结束\') break t = int(x[4:8]) if t in d: print(f\'专业是d[t]\') elif t not in d: print(\'不在这些专业中...\')
运行截图
task5
实验源码
import random lucky_day = random.randint(1, 31) print(\'猜猜哪天会是你的lucky day\', chr(0x1f604)) i = 1 x = list(range(1,32)) d = 1: \'你有三次机会,猜吧(0~31):\', 2: \'再猜(1~31):\', 3: \'再猜(1~31):\' while i <= 4: if i == 4: print(f\'哇哦,次数用光啦\\n偷偷告诉你,5月你的lucky day是lucky_day号.good lucky\', chr(0x1f495)) break day = int(input(d[i])) if day in x: if day == lucky_day: print(\'哇,猜中了\', chr(0x1f973)) break elif day > lucky_day: print(\'猜晚了,你的lucky day已经过了\') elif day < lucky_day: print(\'猜早了,你的lucky day还没到呢\') else: print(\'地球上没有这一天啦,你是外星人吗\') i += 1
运行截图
task6
实验源码
datas = \'2049777001\': [\'篮球\', \'羽毛球\', \'美食\', \'漫画\'], \'2049777002\': [\'音乐\', \'旅行\'], \'2049777003\': [\'马拉松\', \'健身\', \'游戏\'], \'2049777004\': [], \'2049777005\': [\'足球\', \'阅读\'], \'2049777006\': [\'发呆\', \'闲逛\'], \'2049777007\': [], \'2049777008\': [\'书法\', \'电影\'], \'2049777009\': [\'音乐\', \'阅读\', \'电影\', \'漫画\'], \'2049777010\': [\'数学\', \'推理\', \'音乐\', \'旅行\']
d = j = len(datas) for i in range(2049777001, 2049777011): ward = datas[str(i)] for t in ward: d[t] = d.get(t, 0) + 1 while j > 0: for key, value in d.items(): if value == j: print(f\'key:value\') j -= 1
运行截图
3task7_1
实验源码
""" 家用电器销售系统 v1.3 """ #欢迎信息 print(\'欢迎使用家电销售系统!\') #商品数据初始化 products = [ [\'0001\', \'电视机\', \'海尔\', 5999.00, 20], [\'0002\', \'冰箱\', \'西门子\', 6998.00, 15], [\'0003\', \'洗衣机\', \'小天鹅\', 1999.00, 10], [\'0004\', \'空调\', \'格力\', 3900.00, 0], [\'0005\', \'热水器\', \'美的\', 688.00, 30], [\'0006\', \'笔记本\', \'联想\', 5699.00, 10], [\'0007\', \'微波炉\', \'苏泊尔\', 480.00, 33], [\'0008\', \'投影仪\', \'松下\', 1250.00, 12], [\'0009\', \'吸尘器\', \'飞利浦\', 999.00, 9] ] #初始化用户购物车 products_cart = [] option = input(\'请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账\') while option in [\'1\', \'2\', \'3\']: if option == \'1\': #产品信息列表 print(\'商品价格信息表如下:\') print(\'*\' * 50) print(\'%-10s\' % \'编号\', \'%-10s\' % \'名称\', \'%-10s\' % \'品牌\', \'%-10s\' % \'价格\', \'%-10s\' % \'库存数量\') print(\'-\' * 51) for i in range(len(products)): print(\'%-10s\' % products[i][0], \'%-10s\' % products[i][1], \'%-10s\' % products[i][2], \'%10.2f\' % products[i][3], \'%10d\' % products[i][4]) print(\'-\' * 51) elif option == \'2\': products_id = input(\'请输入您要购买的产品编号:\') while products_id not in [item[0] for item in products]: products_id = input(\'编号不存在,请重新输入您要购买的产品编号:\') count = int(input(\'请输入您要购买的产品数量:\')) while count > products[int(products_id)-1][4]: count = int(input(\'库存超出数量,请重新输入您要购买的产品数量:\')) #将所购买的商品加入购物车 if products_id not in [item[0] for item in products_cart]: products_cart.append([products_id, count]) else: for i in range(len(products_cart)): if products_cart[i][0] == products_id: products_cart[i][1] += count #更新商品列表 for i in range(len(products)): if products[i][0] == products_id: products[i][4] -= count else: print(\'购物车信息如下:\') print(\'*\' * 50) print(\'%-10s\' % \'编号\', \'%-10s\' % \'购买数量\') print(\'-\' * 51) for i in range(len(products_cart)): print(\'%-10s\' % products_cart[i][0], \'%6d\' % products_cart[i][1]) print(\'-\' * 51) option = input(\'请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账\') #计算金额 if len(products_cart) > 0: amount = 0 for i in range(len(products_cart)): products_index = 0 for j in range(len(products)): if products_cart[i][0] == products[j][0]: products_index = j break price = products[products_index][3] count = products_cart[i][1] amount += price*count if 5000 < amount <= 10000: amount *= 0.95 elif 10000 < amount <= 20000: amount *= 0.90 elif amount > 20000: amount *= 0.85 print(\'购买成功,您需要支付%8.2f元\' % amount) #退出系统 print(\'谢谢您的光临,下次再见!\')
运行截图
3task7_2
实验源码
""" 家用电器销售系统 v1.3 """ #欢迎信息 print(\'欢迎使用家电销售系统!\') #商品数据初始化 products = [ [\'0001\', \'电视机\', \'海尔\', 5999.00, 20], [\'0002\', \'冰箱\', \'西门子\', 6998.00, 15], [\'0003\', \'洗衣机\', \'小天鹅\', 1999.00, 10], [\'0004\', \'空调\', \'格力\', 3900.00, 0], [\'0005\', \'热水器\', \'美的\', 688.00, 30], [\'0006\', \'笔记本\', \'联想\', 5699.00, 10], [\'0007\', \'微波炉\', \'苏泊尔\', 480.00, 33], [\'0008\', \'投影仪\', \'松下\', 1250.00, 12], [\'0009\', \'吸尘器\', \'飞利浦\', 999.00, 9] ] #初始化用户购物车 products_cart = [] option = input(\'请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账\') while option in [\'1\', \'2\', \'3\']: if option == \'1\': #产品信息列表 print(\'商品价格信息表如下:\') print(\'*\' * 50) print(f\'"编号":10s"名称":10s"品牌":10s"价格":10s"库存数量":10s\') print(\'-\' * 51) for i in range(len(products)): print(f\'products[i][0]:10products[i][1]:10products[i][2]:10products[i][3]:10products[i][4]:10\') print(\'-\' * 51) elif option == \'2\': products_id = input(\'请输入您要购买的产品编号:\') while products_id not in [item[0] for item in products]: products_id = input(\'编号不存在,请重新输入您要购买的产品编号:\') count = int(input(\'请输入您要购买的产品数量:\')) while count > products[int(products_id)-1][4]: count = int(input(\'库存超出数量,请重新输入您要购买的产品数量:\')) #将所购买的商品加入购物车 if products_id not in [item[0] for item in products_cart]: products_cart.append([products_id, count]) else: for i in range(len(products_cart)): if products_cart[i][0] == products_id: products_cart[i][1] += count #更新商品列表 for i in range(len(products)): if products[i][0] == products_id: products[i][4] -= count else: print(\'购物车信息如下:\') print(\'*\' * 50) print(f\'"编号":10"购买数量":10\') print(\'-\' * 51) for i in range(len(products_cart)): print(f\'products_cart[i][0]products_cart[i][1]\') print(\'-\' * 51) option = input(\'请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账\') #计算金额 if len(products_cart) > 0: amount = 0 for i in range(len(products_cart)): products_index = 0 for j in range(len(products)): if products_cart[i][0] == products[j][0]: products_index = j break price = products[products_index][3] count = products_cart[i][1] amount += price*count if 5000 < amount <= 10000: amount *= 0.95 elif 10000 < amount <= 20000: amount *= 0.90 elif amount > 20000: amount *= 0.85 print(f\'购买成功,您需要支付amount:8.2f元\') #退出系统 print(\'谢谢您的光临,下次再见!\')
运行截图
3task8_1
实验源码
""" 家用电器销售系统 v1.4 """ #欢迎信息 print(\'欢迎使用家电销售系统!\') #商品数据初始化 products=[ \'id\':\'0001\',\'name\':\'电视机\',\'brand\':\'海尔\',\'price\':5999.00,\'count\':20, \'id\':\'0002\',\'name\':\'冰箱\',\'brand\':\'西门子\',\'price\':6998.00,\'count\':15, \'id\':\'0003\',\'name\':\'洗衣机\',\'brand\':\'小天鹅\',\'price\':1999.00,\'count\':10, \'id\':\'0004\',\'name\':\'空调\',\'brand\':\'格力\',\'price\':3900.00,\'count\':0, \'id\':\'0005\',\'name\':\'热水器\',\'brand\':\'格力\',\'price\':688.00,\'count\':30, \'id\':\'0006\',\'name\':\'笔记本\',\'brand\':\'联想\',\'price\':5699.00,\'count\':10, \'id\':\'0007\',\'name\':\'微波炉\',\'brand\':\'苏泊尔\',\'price\':480.00,\'count\':33, \'id\':\'0008\',\'name\':\'投影仪\',\'brand\':\'松下\',\'price\':1250.00,\'count\':12, \'id\':\'0009\',\'name\':\'吸尘器\',\'brand\':\'飞利浦\',\'price\':999.00,\'count\':9, ] #初始化用户购物车 products_cart = [] option = input(\'请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账\') while option in [\'1\', \'2\', \'3\']: if option == \'1\': #产品信息列表 print(\'商品价格信息表如下:\') print(\'*\' * 50) print(\'%-10s\' % \'编号\', \'%-10s\' % \'名称\', \'%-10s\' % \'品牌\', \'%-10s\' % \'价格\', \'%-10s\' % \'库存数量\') print(\'-\' * 51) for i in range(len(products)): print(\'%-10s\' % products[i][\'id\'], \'%-10s\' % products[i][\'name\'], \'%-10s\' % products[i][\'brand\'], \'%10.2f\' % products[i][\'price\'], \'%10d\' % products[i][\'count\']) print(\'-\' * 51) elif option == \'2\': products_id = input(\'请输入您要购买的产品编号:\') while products_id not in [item[\'id\'] for item in products]: products_id = input(\'编号不存在,请重新输入您要购买的产品编号:\') count = int(input(\'请输入您要购买的产品数量:\')) while count > products[int(products_id)-1][\'count\']: count = int(input(\'库存超出数量,请重新输入您要购买的产品数量:\')) #将所购买的商品加入购物车 if products_id not in [item[\'id\'] for item in products_cart]: products_cart.append(\'id\': products_id, \'count\': count) else: for i in range(len(products_cart)): if products_cart[i][\'id\'] == products_id: products_cart[i][\'count\'] += count #更新商品列表 for i in range(len(products)): if products[i][\'id\'] == products_id: products[i][\'count\'] -= count else: print(\'购物车信息如下:\') print(\'*\' * 50) print(\'%-10s\' % \'编号\', \'%-10s\' % \'购买数量\') print(\'-\' * 51) for i in range(len(products_cart)): print(\'%-10s\' % products_cart[i][\'id\'], \'%6d\' % products_cart[i][\'count\']) print(\'-\' * 51) option = input(\'请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账\') #计算金额 if len(products_cart) > 0: amount = 0 for i in range(len(products_cart)): products_index = 0 for j in range(len(products)): if products_cart[i][\'id\'] == products[j][\'id\']: products_index = j break price = products[products_index][\'price\'] count = products_cart[i][\'count\'] amount += price*count if 5000 < amount <= 10000: amount *= 0.95 elif 10000 < amount <= 20000: amount *= 0.90 elif amount > 20000: amount *= 0.85 print(\'购买成功,您需要支付%8.2f元\' % amount) #退出系统 print(\'谢谢您的光临,下次再见!\')
运行截图
3task8_2
实验源码
""" 家用电器销售系统 v1.4 """ #欢迎信息 print(\'欢迎使用家电销售系统!\') #商品数据初始化 products=[ \'id\':\'0001\',\'name\':\'电视机\',\'brand\':\'海尔\',\'price\':5999.00,\'count\':20, \'id\':\'0002\',\'name\':\'冰箱\',\'brand\':\'西门子\',\'price\':6998.00,\'count\':15, \'id\':\'0003\',\'name\':\'洗衣机\',\'brand\':\'小天鹅\',\'price\':1999.00,\'count\':10, \'id\':\'0004\',\'name\':\'空调\',\'brand\':\'格力\',\'price\':3900.00,\'count\':0, \'id\':\'0005\',\'name\':\'热水器\',\'brand\':\'格力\',\'price\':688.00,\'count\':30, \'id\':\'0006\',\'name\':\'笔记本\',\'brand\':\'联想\',\'price\':5699.00,\'count\':10, \'id\':\'0007\',\'name\':\'微波炉\',\'brand\':\'苏泊尔\',\'price\':480.00,\'count\':33, \'id\':\'0008\',\'name\':\'投影仪\',\'brand\':\'松下\',\'price\':1250.00,\'count\':12, \'id\':\'0009\',\'name\':\'吸尘器\',\'brand\':\'飞利浦\',\'price\':999.00,\'count\':9, ] #初始化用户购物车 products_cart = [] option = input(\'请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账\') while option in [\'1\', \'2\', \'3\']: if option == \'1\': #产品信息列表 print(\'商品价格信息表如下:\') print(\'*\' * 50) print(f\'"编号":10s"名称":10s"品牌":10s"价格":10s"库存数量":10s\') print(\'-\' * 51) for i in range(len(products)): print(f\'products[i]["id"]:10products[i]["name"]:10products[i]["brand"]:10products[i]["price"]:10\' f\'products[i]["count"]:10\') print(\'-\' * 51) elif option == \'2\': products_id = input(\'请输入您要购买的产品编号:\') while products_id not in [item[\'id\'] for item in products]: products_id = input(\'编号不存在,请重新输入您要购买的产品编号:\') count = int(input(\'请输入您要购买的产品数量:\')) while count > products[int(products_id)-1][\'count\']: count = int(input(\'库存超出数量,请重新输入您要购买的产品数量:\')) #将所购买的商品加入购物车 if products_id not in [item[\'id\'] for item in products_cart]: products_cart.append(\'id\': products_id, \'count\': count) else: for i in range(len(products_cart)): if products_cart[i][\'id\'] == products_id: products_cart[i][\'count\'] += count #更新商品列表 for i in range(len(products)): if products[i][\'id\'] == products_id: products[i][\'count\'] -= count else: print(\'购物车信息如下:\') print(\'*\' * 50) print(f\'"编号":10"购买数量":10\') print(\'-\' * 51) for i in range(len(products_cart)): print(\'%-10s\' % products_cart[i][\'id\'], \'%6d\' % products_cart[i][\'count\']) print(\'-\' * 51) option = input(\'请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账\') #计算金额 if len(products_cart) > 0: amount = 0 for i in range(len(products_cart)): products_index = 0 for j in range(len(products)): if products_cart[i][\'id\'] == products[j][\'id\']: products_index = j break price = products[products_index][\'price\'] count = products_cart[i][\'count\'] amount += price*count if 5000 < amount <= 10000: amount *= 0.95 elif 10000 < amount <= 20000: amount *= 0.90 elif amount > 20000: amount *= 0.85 print(f\'购买成功,您需要支付amount:8.2f元\') #退出系统 print(\'谢谢您的光临,下次再见!\')
运行截图
数据库原理与应用实验5--[数据库的组合和统计查询]
一、实验目的
使学生进一步掌握SQL Server查询的使用方法,加深对T-SQL语言查询语句的理解。熟练掌握数据查询中的分组、统计、计算和组合的操作方法。
二、实验过程及分析
1.实验内容
1、分组查询实验。该实验包括分组条件表达、选择组条件的表达方法。
2、使用函数查询的实验。该实验包括统计函数和分组统计函数的使用方法。
3、组合查询实验。
4、计算和分组计算查询的实验。
2.实验过程
在图书借阅数据库中实现下面的查询操作。
图书(书号,类别,出版社,作者,书名,定价,库存量);
读者(读者编号,姓名,单位,性别,电话);
借阅(书号,读者编号,借书日期,还书日期);
1)查找每个出版社所出版图书的最高价。
2)查找借阅图书超过3本的读者编号和借书册数,要求只统计2019-12-10以后的借书情况。
3)查找读者的借阅情况,即读者编号、借书册数,并进行借书情况汇总。
4)求机械工业出版社出版的各类图书的平均定价,用GROUP BY表示。
5)按照年份统计每个读者的借书册数,并进行汇总。
6)查询借阅了书号为“j0001”或“j0002”的读者编号。
7)查询既借阅了书号为“j0001”,又借阅了书号为“j0002”的读者编号。
8)查询借阅了书号为“j0001”,但没有借阅书号为“j0002”的读者编号。
9)查找这样的图书类别:要求类别中最高的图书定价不低于按类别分组的图书平均定价的2倍。
3.实验分析
可以顺利完成实验;在实验中对于count函数运用不是很熟练。
三、实验总结
可以在SQL Server Management Studio新建查询的输入区中输入T-SQL查询语句;可设置 Query Analyzer的结果区为Standard Execute(标准执行)或Execute to Grid(网格执行)方式;发布执行命令,并在结果区中查看查询结果;如果结果不正确,要进行修改,直到正确为止。
以上是关于实验三 控制语句与组合数据类型应用编程的主要内容,如果未能解决你的问题,请参考以下文章