首页 > 其他分享 >实验三

实验三

时间:2023-04-25 21:36:06浏览次数:26  
标签:count product cart products print 实验 id

1.实验任务1

task1.py

程序源代码:

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

 

运行程序截图:

 

关于问题回答:

(1)问题1:(0,100)可以

(2)问题2:一、list(range(5))的范围是[0,4],不包括5

                 二、list(range(1,5))的范围是[1,4],也不包括5

(3)问题3:不一定,取不到0

(4)问题4:一定

 

2.实验任务2

task2-1.py

程序源代码:

 1 lst =[55,92,88,79,96]
 2 i =0
 3 while i<len(lst):
 4  print(lst[i],end = ' ') 
 5  i+=1
 6 print()
 7 for i in range(len(lst)):
 8  print(lst[i],end=' ')
 9 print()
10 for i in lst:
11  print(i,end=' ')
12 print()

 

运行程序截图:

 

 

task2-2.py

程序源代码:

 1 book_info ={'isbn':'978-7-5356-8297-0','书名':'白鲸记','作者':'克里斯多夫·夏布特','译者':'高文婧','出版社':'湖南美术出版社','售价':82}
 2 
 3 for key,value in book_info.items():
 4     print(f'{key}:{value}')
 5 print()
 6 
 7 for item in book_info.items():
 8     print(f'{item[0]}:{item[1]}')
 9 print()
10 
11 for value in book_info.values():
12     print(value,end=' ')
13 print()
14 
15 for key in book_info.keys():
16     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('{}.'.format(i+1),end='')
10     new_dict = book_infos[i]
11     for value in new_dict.values():
12         if value ==new_dict['书名']:
13             print('《{}》,'.format(value),end='')
14         else:
15             print(value)
16     i+=1

 

运行程序截图:

 

3.实验任务3

task3.py

程序源代码:

 1 import string
 2 ts = '''The Zen of Python, by Tim Peters
 3 
 4 Beautiful is better than ugly.
 5 Explicit is better than implicit.
 6 Simple is better than complex.
 7 Complex is better than complicated.
 8 Flat is better than nested.
 9 Sparse is better than dense.
10 Readability counts.
11 Special cases aren't special enough to break the rules.
12 Although practicality beats purity.
13 Errors should never pass silently.
14 Unless explicitly silenced.
15 In the face of ambiguity, refuse the temptation to guess.
16 There should be one-- and preferably only one --obvious way to do it.
17 Although that way may not be obvious at first unless you're Dutch.
18 Now is better than never.
19 Although never is often better than *right* now.
20 If the implementation is hard to explain, it's a bad idea.
21 If the implementation is easy to explain, it may be a good idea.
22 Namespaces are one honking great idea -- let's do more of those!
23 '''
24 newts = ts.lower()
25 d ={}
26 ls1 = []
27 ls2 = []
28 for i in string.ascii_lowercase:
29     n = newts.count(i)
30     ls1.append(i)
31     ls2.append(n)
32 for i in range(1,len(ls1)):
33     for n in range(0,len(ls1)-i):
34         if ls2[n]<ls2[n+1]:
35             ls2[n],ls2[n+1]=ls2[n+1],ls2[n]
36             ls1[n],ls1[n+1]=ls1[n+1],ls1[n]
37 i=0
38 while i<len(ls1):
39     print('{}:{}'.format(ls1[i],ls2[i]))
40     i+=1

 

运行程序截图:

 

4.实验任务4

task4.py

运行源代码:

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

 

程序运行截图:

 

 

5.实验任务5

task5.py

实验源代码:

 1 import random
 2 re_day =  random.randint(1,31)
 3 print('猜猜2023年5月哪一天会是你的lucky day

标签:count,product,cart,products,print,实验,id
From: https://www.cnblogs.com/nettj666/p/17332808.html

相关文章

  • 实验3 控制语句与组合数据类型应用编程
    task1.pyimportrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()whi......
  • python实验笔记1
    1.python如何在一行里面输入两个数呢如果直接这样子写会报错n=int(input())m=int(input())要按照下面的写法才可以实现n,m=map(int,input().split())2.python实现排列组合在itertools库中提供了两个函数permutations和combinations可以实现全排列和组......
  • 实验三
    importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()whilelen(......
  • 山东大学数据结构实验七
    卡片游戏tips:这个题还要参考,同学要加油啦~~要求创建队列类,使用数组描述的循环队列实现卡片游戏描述假设桌上有一叠扑克牌,依次编号为1-n(从上至下)。当至少还有两张的时候,可以进行操作:把第一张牌扔掉,然后把新的第一张(原先扔掉的牌下方的那张牌,即第二张牌)放到整叠牌的最后。......
  • 实验三
    #1实验内容:1#12importrandom3print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('\n用集......
  • 山东大学数据结构实验六
    计算表达式tips:不要全文复制,会被查重哦注意因为精度问题,请使用double存数据。要求创建栈类,采用数组描述;计算数学表达式的值。输入数学表达式,输出表达式的计算结果。数学表达式由单个数字和运算符+、-、*、/、(、)构成,例如2+3*(4+5)-6/4。假定表达式输入格式合法。格式......
  • 山东大学数据结构实验一(2)
    题目描述现有一个有n个元素的序列\(a=[a_{1},a_{2},\cdots,a_{n}]\),定义其价值为\(\sum_{i=1}^{n}a_{i}\oplusi\)给出这样一个序列,求其所有排列的价值\(v_{i}\)的或\(v_{1}|v_{2}|\cdots|v_{n-1}|v_{n}\)其中\(|\)为位运算或操作,\(\oplus\)为位运算异......
  • 山东大学数据结构实验一(1)
    题目描述现有一个有\(n\)个元素的序列\(a=[a_1,a_2,\cdots,a_n]\),定义这个序列的价值为\(\sum_{i=1}^{n}i\timesa_i\)。空序列的价值为\(0\)。先给你一个长度为\(n\)的序列\(a\),求\(a\)中所有子集价值的异或和,要求子集中元素的相对位置保持不变。异或和:位运算的一种。如果a......
  • 实验三
    任务一实验源码运行结果截图任务二实验源码1lst=[55,92,88,79,96]23i=04whilei<len(lst):5print(lst[i],end='')6i+=17print()89foriinrange(len(lst)):10print(lst[i],end='')11print()1213forii......
  • 实验3 控制语句与组合数据类型应用编程
    实验任务1实验源码1importrandom23print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('......