首页 > 编程语言 >实验3 控制语句与组合数据类型应用编程

实验3 控制语句与组合数据类型应用编程

时间:2023-04-26 21:47:38浏览次数:41  
标签:语句 遍历 编程 数据类型 number value book key print

1。实验任务1

task1.py

 1 import random
 2 
 3 print('用列表储存随机整数:')
 4 lst = [random.randint(0,100) for i in range(5)]
 5 print(lst)
 6 
 7 print('\n用集合储存随机整数:')
 8 s1 = {random.randint(0,100) for i in range(5)}
 9 print(s1)
10 
11 print('\n用集合存储随机整数:')
12 s2 = set()
13 while len(s2) < 5:
14     s2.add(random.randint(0,100))
15 print(s2)

2.实验任务2

task2_1.py

 1 # 列表遍历
 2 lst = [55,92,88,79,96]
 3  #遍历方式1:使用while + 索引
 4 i = 0
 5 while i < len(lst):
 6     print(lst[i],end = ' ')
 7     i+=1
 8 
 9 print()
10 
11  #遍历方式2:使用for + 索引
12 for i in range(len(lst)):
13     print(lst[i],end = ' ')
14 print()
15 
16 #遍历方式3:使用for...in
17 for i in lst:
18     print(i,end = ' ')
19 print()

task2_2.py

 1 # 字典遍历
 2 book_info = {'isbn': '978-7-5356-8297-0',
 3              '书名': '白鲸记',
 4              '作者': '克里斯多夫.夏布特',
 5              '译者': '高文婧',
 6              '出版社': '湖南美术出版社',
 7              '售价': 82
 8              }
 9 
10 # 遍历key-value对: 实现方式1
11 for key,value in book_info.items():
12     print(f'{key}:{value}')
13 print()
14 
15 #遍历key-value对: 实现方式2
16 for item in book_info.items():
17     print(f'{item[0]}:{item[1]}')
18 print()
19 
20 #遍历值: 实现方式1
21 for value in book_info.values():
22     print(value, end = ' ')
23 print()
24 
25 #遍历值:实现方式2
26 for key in book_info.keys():
27     print(book_info[key],end = ' ')

task2_3.py

 1 book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'},
 2               {'书名': '局外人', '作者': '阿尔贝.加缪'},
 3               {'书名': '设计中的设计', '作者': '原研哉'},
 4               {'书名': '万历十五年', '作者': '黄仁宇'},
 5               {'书名': '刀锋', '作者': '毛姆'}
 6               ]
 7 i = 0
 8 while i < len(book_infos):
 9     print(i+1,end ='. ')
10     x = []
11     for value in book_infos[i].values():
12         x.append(value)
13     print(f'《{x[0]}》, {x[1]}')
14     i+=1

 3.实验任务3

task3.py

 1 text = '''The Zen of Python, by Tim Peters
 2 
 3 Beautiful is better than ugly.
 4 Explicit is better than implicit.
 5 Simple is better than complex.
 6 Complex is better than complicated.
 7 Flat is better than nested.
 8 Sparse is better than dense.
 9 Readability counts.
10 Special cases aren't special enough to break the rules.
11 Although practicality beats purity.
12 Errors should never pass silently.
13 Unless explicitly silenced.
14 In the face of ambiguity, refuse the temptation to guess.
15 There should be one-- and preferably only one --obvious way to do it.
16 Although that way may not be obvious at first unless you're Dutch.
17 Now is better than never.
18 Although never is often better than *right* now.
19 If the implementation is hard to explain, it's a bad idea.
20 If the implementation is easy to explain, it may be a good idea.
21 Namespaces are one honking great idea -- let's do more of those!'''
22 
23 text1 = text.lower()
24 alpha_dict = {}
25 for i in range(97,123):
26     alpha = chr(i)
27     number = text1.count(alpha)
28     alpha_dict[alpha] = number
29 ans = sorted(alpha_dict.items(),key=lambda x:x[1],reverse = True)
30 for j in ans:
31     print(f'{j[0]}:{j[1]}')

 4.实验任务4

task4.py

 1 code_majors = {'8326': '地信类',
 2                '8329': '计算机类',
 3                '8330': '气科类',
 4                '8336': '防灾工程',
 5                '8345': '海洋科学',
 6                '8382': '气象工程'
 7                }
 8 
 9 print('{:-^50}'.format('专业代号信息'))
10 for key,value in code_majors.items():
11     print(f'{key}:{value}')
12 print('{:-^50}'.format('学生专业查询'))
13 stu_number = input('请输入学号:')
14 
15 while True:
16     if stu_number[4:8] in code_majors:
17         x = code_majors[stu_number[4:8]]
18         print(f'专业是:{x}' )
19         stu_number = input('请输入学号:')
20     elif stu_number == '#':
21         print('查询结束...')
22         break
23     else:
24         print('不在这些专业中...')
25         stu_number = input('请输入学号:')

5.实验任务5

task5.py

 1 import random
 2 lucky_day = random.randint(1,31)
 3 print('猜猜2023年5月哪一天会是你的lucky day' + chr(0x1f600))
 4 number = int(input('你有三次机会,猜吧(1~31):'))
 5 i = 1
 6 while True:
 7         if number >31 or number <1:
 8             print('地球上没有这一天啦,你是外星人吗')
 9             i+=1
10             if i == 4:
11                 print('哇哦,次数用光啦.')
12                 print(f'偷偷告诉你,5月你的lucky day是 {lucky_day} 号.good luck' + chr(0x1f601) )
13                 break
14             number = int(input('再猜(1~31):'))
15 
16 
17         elif number < lucky_day:
18             print('猜早啦,你的lucky day还没到呢')
19             i+=1
20             if i == 4:
21                 print('哇哦,次数用光啦.')
22                 print(f'偷偷告诉你,5月你的lucky day是 {lucky_day} 号.good luck' + chr(0x1f601) )
23                 break
24             number = int(input('再猜(1~31):'))
25 
26 
27         elif number > lucky_day:
28             print('猜晚啦,你的lucky day已经过去啦')
29             i+=1
30             if i == 4:
31                 print('哇哦,次数用光啦.')
32                 print(f'偷偷告诉你,5月你的lucky day是 {lucky_day} 号.good luck' + chr(0x1f601) )
33                 break
34             number = int(input('再猜(1~31):'))
35 
36 
37         elif number == lucky_day:
38             print('哇,猜中了' + chr(0x1f602))
39             break

 

标签:语句,遍历,编程,数据类型,number,value,book,key,print
From: https://www.cnblogs.com/wang2003chengxu/p/17332813.html

相关文章

  • ORM执行SQL语句,双下__查询,多表查询,外建字段增删改查
    目录ORM执行SQL语句查看原生sql语句神奇的双下划线查询ORM外键字段的创建一对多的外键增删改查数据多对多外键增删改查ORM跨表查询基于对象的跨表查询基于上下划线的跨表查询进阶操作ORM执行SQL语句有时候ORM的操作效率可能偏低我们是可以自己编写SQL的方式1: models.User.o......
  • 2023.4.26编程一小时打卡
    一、问题描述:有一元二次方程ax2+bx+c=0,其一般解为x1,2=(-b±b2-4ac)/2a,但若a=0或b2-4ac<0时,用此公式出错。编程序,从键盘输入a,b,c的值,求x1和x2。如果a=0或b2-4ac<0,输出出错信息。二、解题思路:首先,将定义a,b,c为浮点数,然后输入a,b,c,去判断二次项系数的大小是否符合,再去判断b*2......
  • Django模型层(一) (测试环境搭配 常见的十几种查询方法-ORM关键字 ORM执行SQL语句
    目录一、测试环境搭配切换数据库自带的sqlite3数据库对时间字段不敏感有时候会展示错乱,所以我们习惯切换成常见的数据库比如MySQLdjangoorm并不会自动帮你创建库,所以需要提前准备好!单独搭配测试环境单独测试django某个功能层,默认不允许单独测试某个py文件,如果想要测试......
  • navicat删除表中重复数据的sql语句
    DELETEFROM`hao123`WHEREir_urlIN(SELECTir_urlFROM(SELECTir_urlFROM`hao123`GROUPBYir_urlhavingcount(ir_url)>1)a)ANDir_idNOTIN(SELECT*FROM(SELECTmin(ir_id)FROM`hao123`GROUPBYir_urlHAVINGcount(ir_url)>1)b);hao123:表名。ir_......
  • c语言趣味编程(8)冒泡排序
    一、问题描述对n个整数进行升序排列(数据由键盘键入)二、设计思路(1)读取从键盘键入的n个数据,存入数组a中;(2)用for循环对数组a中的数据进行遍历,并用if语句来判断大小,按从小到大存入;(3)遍历输出数组中已经完成升序排列的数组元素;三、程序流程图 四、伪代码五、代码1#include......
  • PVD-CH32可编程电压控制器PVD中断的使用方法
    可编程电压监视器PVD,主要被用于监控系统主电源的变化,与电源控制寄存器PWR_CTLR的PLS[2:0]所设置的门槛电压相比较,配合外部中断寄存器(EXTI)设置,可产生相关中断,以便及时通知系统进行数据保存等掉电前操作。配置方法如下:先使能PWR时钟,然后设置电压监视阈值,随后使能PVDE开......
  • 计算sql查询语句需要的时间
    可以使用SQLServer的内置函数DATEDIFF来检测查询所用的时间。具体方法是,在查询开始之前获取当前的时间,并在查询结束时再次获取当前时间,将两个时间进行DATEDIFF计算即可得到查询所用的时间。例如:DECLARE@start_timeDATETIME;DECLARE@end_timeDATETIME;SET@start_time=GETD......
  • 第六章:流程控制语句
    学习要点:1.语句的定义2.if语句3.switch语句4.do...while语句5.while语句6.for语句7.for...in语句8.break和continue语句9.with语句ECMA-262规定了一组流程控制语句。语句定义了ECMAScript中的主要语法,语句通常由一个或者多个关键字来完成给定的任务。诸如:判断、循环......
  • 编程打卡:来玩玩Ruby语言吧2.1!
    编程打卡:来玩玩Ruby语言吧2.1!我们前面实现了一个有趣的树类Tree,但它不具有简洁的用户接口,来设置一棵新树,为它写一个初始化方法,接受散列表和数组嵌套的结构。写好之后,你可以这样设置新树:{'grandpa'=>{'dad'=>{'child1'=>{},'child2'=>{}},'uncle'=>{'child3�......
  • C#初级编程
    1.作为行为组件的脚本usingUnityEngine;usingSystem.Collections;publicclassExampleBehaviourScript:MonoBehaviour{voidUpdate(){if(Input.GetKeyDown(KeyCode.R)){GetComponent<Renderer>().material.color=Color.......