首页 > 其他分享 >实验二

实验二

时间:2023-03-28 22:45:58浏览次数:30  
标签:%- 10 list len 实验 print 10s

实验任务1

 1 x = 'nba FIFA'
 2 print(x.upper())
 3 print(x.lower())
 4 print(x.swapcase())
 5 print()
 6 
 7 x = 'abc'
 8 print(x.center(10,'*'))
 9 print(x.ljust(10,'*'))
10 print(x.rjust(10,'*'))
11 print()
12 
13 x = '123'
14 print(x.zfill(10))
15 x = 123
16 print(str(x).zfill(10))
17 print()
18 
19 x = 'phonr_number'
20 print(x.isidentifier())
21 x = '222test'
22 print(x.isidentifier())
23 print()
24 
25 x = '  '
26 print(x.isspace())
27 x = '\n'
28 print(x.isspace())
29 print()
30 
31 x = 'python is fun'
32 table = x.maketrans('thon','1234')
33 print(x.translate(table))
1

运行结果截图

实验任务2

 1 x = [5,11,9,7,42]
 2 print('整数输出1:',end = '')
 3 i=0
 4 while i <len(x):
 5     print(x[i],end = ' ')
 6     i+=1
 7 
 8 print('\n整数输出2:',end = '')
 9 i=0
10 while i<len(x):
11     print(f'{x[i]:02d}',end = '-')
12     i+=1
13 
14 print('\n整数输出3:',end = '')
15 i=0
16 while i<len(x)-1:
17     print(f'{x[i]:02d}',end = '-')
18     i+=1
19 print(f'{x[-1]:02d}')
20 
21 print('\n字符输出1:',end = '')
22 y1 = []
23 i = 0
24 while i <len(x):
25     y1.append(str(x[i]))
26     i+=1
27 print('-'.join(y1))
28 
29 
30 print('字符输入2:',end= '')
31 y2 = []
32 i = 0
33 while i < len(x):
34     y2.append(str(x[i]).zfill(2))
35     i+=1
36 print('-'.join(y2))
2

运行结果截图

实验任务3

 1 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteau twins']
 2 
 3 #方法1
 4 i = 0
 5 while i < len(name_list):
 6     print(name_list[i].title())
 7     i += 1
 8 
 9 print()
10 
11 #方法2
12 t = []
13 i = 0
14 while i < len(name_list):
15     t.append(name_list[i].title())
16     i += 1
17 
18 print('\n'.join(t))
3

运行结果截图

 

实验任务4

1 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteau twins']
2 n = sorted(name_list)
3 i = 0
4 while i < len(name_list):
5     print(f'{i + 1}. {n[i].title()}')
6     i += 1
4

运行结果截图

实验任务5

 1 ZOP = '''The Zen of Python, by Tim Peters
 2 Beautiful is better than ugly.
 3 Explicit is better than implicit.
 4 Simple is better than complex.
 5 Complex is better than complicated.
 6 Flat is better than nested.
 7 Sparse is better than dense.
 8 Readability counts.
 9 Special cases aren't special enough to break the rules.
10 Although practicality beats purity.
11 Errors should never pass silently.
12 Unless explicitly silenced.
13 In the face of ambiguity, refuse the temptation to guess.
14 There should be one-- and preferably only one --obvious way to do it.
15 Although that way may not be obvious at first unless you're Dutch.
16 Now is better than never.
17 Although never is often better than *right* now.
18 If the implementation is hard to explain, it's a bad idea.
19 If the implementation is easy to explain, it may be a good idea.
20 Namespaces are one honking great idea -- let's do more of those!'''
21 print(f'行数:{len(ZOP.splitlines())}')
22 print(f'单词数:{len(ZOP.split())}')
23 print(f'字符数:{len(ZOP)}')
24 print(f'空格数:{ZOP.count(" ")}')
5

运行结果截图

实验任务6

 1 book_list = [['静静的顿河','肖洛霍夫','金人', '人民文学出版社'],
 2 ['大地之上','罗欣顿.米斯特里','张亦琦', '天地出版社'],
 3 ['夜航西飞', '柏瑞尔.马卡姆', '陶立夏', '人民文学出版社'],
 4 ['来自民间的叛逆', '袁越', '','新星出版社'],
 5 ['科技与恶的距离', '珍妮.克里曼', ' 詹蕎語', '墨刻出版社'],
 6 ['灯塔','克里斯多夫.夏布特','吕俊君','北京联合出版公司'],
 7 ['小行星掉在下午','沈大成', '', '广西师范大学出版社']]
 8 print(f"{'图书信息':*^40}")
 9 import copy
10 b = copy.deepcopy(book_list)
11 i = 0
12 while i < len(book_list):
13     a = book_list[i]
14     del(a[2])
15     print(f'{i + 1}. {"|".join(a)}')
16     i += 1
17 book_list = b
6

运行结果截图

实验任务7

 1 data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100']
 2 i = 0
 3 count = 0
 4 ct = 0
 5 while i < len(data):
 6     a = data[i]
 7     b = a.split()
 8     ct += len(b)
 9     x = 0
10     while x < len(b):
11         c = b[x]
12         count += int(c)
13         x += 1
14     i += 1
15 d = count/ct
16 print(f'{d:.2f}')
7

运行结果截图

实验任务8

 1 words_sensitive_list = ['张三', 'V字仇杀队', '杀']
 2 comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。',
 3 '电影<V字仇杀队>从豆瓣下架了',
 4 '娱乐至死']
 5 ls = []
 6 j = 0
 7 while j < len(comments_list):
 8     i = 0
 9     b = comments_list[j]
10     while i < len(words_sensitive_list):
11         a = words_sensitive_list[i]
12         if a in b:
13             b = b.replace(a, '*'*len(a))
14             i += 1
15         else:
16             i += 1
17     ls.append(b)
18     j += 1
19 for i in ls:
20     print(i)
8

运行结果截图

实验任务9-1

 1 """
 2 家用电器销售系统
 3 v1.1
 4 """
 5 
 6 #欢迎信息
 7 print('欢迎使用家用电器销售系统!')
 8 
 9 #产品信息列表
10 print('产品和价格信息如下:')
11 print('*'*57)
12 print('%-10s'%'编号', '%-10s'%'名称', '%-10s'%'品牌', '%-10s'%'价格', '%-10s'%'库存数量')
13 print('-'*51)
14 print('%-10s'%'0001', '%-10s'%'电视机', '%-10s'%'海尔', '%10.2f'%5999.00,'%10d'%20 )
15 print('%-10s'%'0002', '%-10s'%'冰箱', '%-10s'%'西门子', '%10.2f'%6998.00,'%10d'%15 )
16 print('%-10s'%'0003', '%-10s'%'洗衣机', '%-10s'%'小天鹅', '%10.2f'%1999.00,'%10d'%10 )
17 print('%-10s'%'0004', '%-10s'%'空调', '%-10s'%'格力', '%10.2f'%3900.00,'%10d'%0 )
18 print('%-10s'%'0005', '%-10s'%'热水器', '%-10s'%'美的', '%10.2f'%688.00,'%10d'%30 )
19 print('%-10s'%'0006', '%-10s'%'笔记本', '%-10s'%'联想', '%10.2f'%5699.00,'%10d'%10 )
20 print('%-10s'%'0007', '%-10s'%'微波炉', '%-10s'%'苏泊尔', '%10.2f'%480.50,'%10d'%33 )
21 print('%-10s'%'0008', '%-10s'%'投影仪', '%-10s'%'松下', '%10.2f'%1250.00,'%10d'%12 )
22 print('%-10s'%'0009', '%-10s'%'吸尘器', '%-10s'%'飞利浦', '%10.2f'%999.00,'%10d'%9 )
23 print('-'*51)
24 
25 #商品数据
26 products=[
27 ['0001','电视机','海尔',5999.00,20],
28 ['0002','冰箱','西门子',6998.00,15],
29 ['0003','洗衣机','小天鹅',1999.00,10],
30 ['0004','空调','格力',3900.00,0],
31 ['0005','热水器','格力',688.00,30],
32 ['0006','笔记本','联想',5699.00,10],
33 ['0007','微波炉','苏泊尔',480.00,33],
34 ['0008','投影仪','松下',1250.00,12],
35 ['0009','吸尘器','飞利浦',999.00,9]
36 ]
37 
38 #用户输入信息
39 product_id = input('请输入您要购买的产品编号:')
40 count = int(input('请输入您要购买的产品数量:'))
41 
42 #输入编号对应商品的信息
43 product_index = int(product_id) - 1
44 product = products[product_index]
45 
46 #计算金额
47 print('购买成功,您需要支付', product[3]*count, '元')
48 
49 #退出系统
50 print('谢谢您的光临,下次再见!')
9_1

运行结果截图

实验任务9-2

 1 """
 2 家用电器销售系统
 3 v1.1
 4 """
 5 
 6 #欢迎信息
 7 print('欢迎使用家用电器销售系统!')
 8 
 9 #商品数据
10 products=[
11 ['0001','电视机','海尔',5999.00,20],
12 ['0002','冰箱','西门子',6998.00,15],
13 ['0003','洗衣机','小天鹅',1999.00,10],
14 ['0004','空调','格力',3900.00,0],
15 ['0005','热水器','格力',688.00,30],
16 ['0006','笔记本','联想',5699.00,10],
17 ['0007','微波炉','苏泊尔',480.00,33],
18 ['0008','投影仪','松下',1250.00,12],
19 ['0009','吸尘器','飞利浦',999.00,9]
20 ]
21 
22 #产品信息列表
23 print('产品和价格信息如下:')
24 print('*'*57)
25 print(f"{'编号': <10} {'名称': <10} {'品牌': <10} {'价格': <10} {'库存数量': <10} ")
26 for i in products:
27     j = 0
28     while j < len(i):
29         print(i[j], end = ' '*(12 - len(str(i[j]))))
30         j += 1
31     print('',end = '\n')
32 print('-'*51)
33 
34 #用户输入信息
35 product_id = input('请输入您要购买的产品编号:')
36 count = int(input('请输入您要购买的产品数量:'))
37 
38 #输入编号对应商品的信息
39 product_index = int(product_id) - 1
40 product = products[product_index]
41 
42 #计算金额
43 print('购买成功,您需要支付', product[3]*count, '元')
44 
45 #退出系统
46 print('谢谢您的光临,下次再见!')
9_2

运行结果截图

 

标签:%-,10,list,len,实验,print,10s
From: https://www.cnblogs.com/melancholy-z/p/17242757.html

相关文章

  • 实验2
    task1x='nbaFIFA'print(x.upper())print(x.lower())print(x.swapcase())print()x='abc'print(x.center(10,'*'))print(x.ljust(10,'*'))print(x.rjust(10,'*')......
  • 实验2
    实验任务1x='nbaFIFA'print(x.upper())print(x.lower())print(x.swapcase())print()x='abc'print(x.center(10,'*'))print(x.ljust(10,'*'))print(x.rjust(10,......
  • 实验二
    x='nbaFIFA'print(x.upper())print(x.lower())print(x.swapcase())print()x='abc'print(x.center(10,'*'))print(x.ljust(10,'*'))print(x.rjust(10,'*'......
  • 实验二
    任务一实验源码1x='nbaFIFA'2print(x.upper())3print(x.lower())4print(x.swapcase())5print()67x='abc'8print(x.center(10,'*'))9pr......
  • 实验2字符串和列表
    任务1:#字符串的基础操作#课堂上没有演示的一些方法x='nbaFIFA'print(x.upper())#字符串转大写print(x.lower())#字符串转小写print(x.,swapcase())#字符串大小......
  • 实验2 字符串和列表
    实验任务1task1实验源码1#字符串的基础操作2#课堂上没有演示的一些方法34x='nbaFIFA'5print(x.upper())6print(x.lower())7print(x.swapcase()......
  • 实验2
    实验任务1task1.py实验代码:x='nbaFIFA'print(x.upper())print(x.lower())print(x.swapcase())print()x='abc'print(x.center(10,'*'))print(x.ljust(10,......
  • 实验2 字符串和列表
    实验任务1#task1:字符串的基础操作(课堂上没有演示的一些方法)实验内容x='nbaFIFA'print(x.upper())#字符串......
  • HCIP-OSPF实验
      实验要求:1.R4为ISP,其上只能配置IP地址;R4与其他所有直连设备间均使用公有IP2.R3-R5/6/7为MGRE环境,R3为中心站点3.整个OSPF环境IP基于172.16.0.0/16划分4.所有设备......
  • 20203160715-宋晔婷 实验一软件工程准备-软件开发的工程认识
    前文项目内容课程班级博客链接2020级卓越工程师班这个作业要求链接实验一——软件工程准备我的课程学习目标学会使用博客园进行学习学习并掌握Github工......