首页 > 其他分享 >实验2

实验2

时间:2023-03-27 17:36:20浏览次数:29  
标签:%- 10 products format 实验 print 10s

实验任务1

实验源码:

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

实验结果截图:

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

 

实验结果截图:

试验任务3

实验源码:

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

实验结果截图:

实验任务4

实验源码:

 1 name_list=['david bowie','louis armstrong','lenoard cohen','bob dylan','cocteau twins']
 2 y=[]
 3 i=0
 4 while i<len(name_list):
 5     y.append(name_list[i].title())
 6     i+=1
 7 sorted(name_list)
 8 
 9 i=0
10 while i<len(name_list):
11     print(f'{i+1}. {y[i]}')
12     i+=1

实验结果截图:

实验任务5

实验源码:

 

 1 a='''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 b=a.splitlines()
23 print('行数:',len(b))
24 c=a.split()
25 print('单词数:',len(c))
26 d=[]
27 for i in a:
28     d+=list(i)
29 print('字符数:',len(d))
30 print('空格数:',d.count(' '))

实验结果截图:

实验任务六:

实验源码:

 1 book_list=[['静静的顿河','肖洛霍夫','金人','人民文学出版社'],
 2            ['大地之上','罗欣顿.米斯特里','张亦琦','天地出版社'],
 3            ['夜航西飞','伯瑞尔.马卡姆','陶立夏','人民文学出版社'],
 4            ['来自民间的叛逆','袁越','新星出版社'],
 5            ['科技与恶的距离','珍妮.克里曼','张蕎语','墨刻出版社'],
 6            ['灯塔','克里斯多夫.夏补特','吕俊军','北京联合出版社'],
 7            ['小行星掉在下午','沈大成','广西师范出版社']]
 8 print('{:*^40s}'.format('图书信息'))
 9 x=0
10 for i in book_list:
11     y='|'.join(book_list[x])
12     print('{}.{}'.format(x+1,y))
13     x+=1

实验结果截图:

 

 试验任务七:

实验源码:

1 data=['99 81 75','30 42 90 87','69 50 96 77 89 93','82 99 78 100']
2 a=' '.join(data)
3 a=a.split(' ')
4 s=0
5 for i in a:
6     s+=int(i)
7 print('{:.2f}'.format(s/len(a)))

实验结果截图:

实验任务八:

实验源码:

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

实验结果截图:

 试验任务九:

实验源码:

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

实验结果截图:

实验源码:

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

实验结果截图:

 

标签:%-,10,products,format,实验,print,10s
From: https://www.cnblogs.com/wjh857925263/p/17243216.html

相关文章

  • 实验二:字符串和列表
         试验任务一 task1  代码:x='nbaFIFA'print(x.upper())print(x.lower())print(x.swapcase())print()x='abc'print(x.center(10,'*'))p......
  • 实验二 字符串和列表
    实验任务1task1.py1#字符串的基础操作2#课堂上没有演示的一些方法34x='nbaFIFA'5print(x.upper())6print(x.lower())7print(x.swapcase())8......
  • 实验二
    task1源代码1#字符串的基础操作2#课堂上没有演示的一些方法34x='nbaFIFA'5print(x.upper())#字符串转大写6print(x.lower())#字符串转小写7......
  • 实验2 字符串和列表
    实验任务1实验源码:#字符串的基本操作x='nbaFIFA'print(x.upper())print(x.lower())print(x.swapcase())print()x='abc'print(x.center(10,'*'))print(x.lju......
  • 实验2 字符串和列表
    试验任务一task1x='nbaFIFA'print(x.upper())print(x.lower())print(x.swapcase())#字符串大小写翻转print()x='abc'print(x.center(10,'*'))#字......
  • 实验2 目录树的遍历
    Unix实验报告实验:实验2目录树的遍历专业:计算机科学与技术班级:1班姓名:姚怀聿学号:229202022046322022年10月21日目录一......
  • 实验1 同步与异步write的效率比较
    Unix实验报告实验:实验1同步与异步write的效率比较专业:计算机科学与技术班级:1班姓名:姚怀聿学号:229202022046322022年10月7......
  • 实验4 信号处理
    Unix实验报告实验:实验4信号处理专业:计算机科学与技术班级:1班姓名:姚怀聿学号:229202022046322022年12月19日目录一、实......
  • 实验5 哲学家进餐-进程版
    Unix实验报告实验:实验5哲学家进餐-进程版专业:计算机科学与技术班级:1班姓名:姚怀聿学号:229202022046322022年12月2日目录......
  • 实验3 简单shell的设计和实现
    Unix实验报告实验:实验3简单shell的设计和实现专业:计算机科学与技术班级:1班姓名:姚怀聿学号:229202022046322022年11月5日......