首页 > 其他分享 >实验2 字符串和列表

实验2 字符串和列表

时间:2023-03-25 19:33:27浏览次数:33  
标签:%- 10 int 列表 print 实验 字符串 10s data

实验任务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 x='phone_number'
19 print(x.isidentifier())
20 x='222test'
21 print(x.isidentifier())
22 print()
23 
24 x=' '
25 print(x.isspace())
26 x='\n'
27 print(x.isspace)
28 print()
29 
30 x='python is fun'
31 table=x.maketrans('thon','1234')
32 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 print('\n整数输入2:',end='')
 8 i=0
 9 while i<len(x):
10     print(f'{x[i]:02d}',end='')
11     i+=1
12 print('\n输入整数3:',end='')
13 i=0
14 while i<len(x)-1:
15     print(f'{x[i]:02d}',end='-')
16     i+=1
17 print(f'{x[-1]:02d}')
18 print('\n字符输入1:',end='')
19 y1=[]
20 i=0
21 while i<len(x):
22     y1.append(str(x[i]))
23     i+=1
24 print('-'.join(y1))
25 print('字符输出2:',end='')
26 y2=[]
27 i=0
28 while i<len(x):
29     y2.append(str(x[i]).zfill(2))
30     i+=1
31 print('_'.join(y2))

运行截图

实验任务3

源代码

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

运行截图

实验任务4

源代码

1 name_list=['david bowie','louis armstron','leonard cohen','bob dylan','cocteau twins']
2 name_list.sort()  #姓名按字典序编号输出
3 i=0
4 while i<len(name_list):
5     print(i+1,'.',name_list[i].title())
6     i+=1
7 print()

运行截图

实验任务5

源代码

 

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

运行截图

实验任务6

源代码

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

运行截图

实验任务7

源代码

 1 '''
 2 某.csv格式数据文件内数据如下:
 3 99 81 75
 4 30 42 90 87
 5 69 50 96 77 89, 93
 6 82, 99, 78, 100
 7 '''
 8 data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100']
 9 title='Remote Interpreter Reinitialized'
10 print(title.center(30,'*'))
11 x1=int(data[0][0:2])+int(data[0][3:5])+int(data[0][6:8])
12 x2=int(data[1][0:2])+int(data[1][3:5])+int(data[1][6:8])+int(data[1][9:11])
13 x3=int(data[2][0:2])+int(data[2][3:5])+int(data[2][6:8])+int(data[2][9:11])+int(data[2][12:14])+int(data[2][15:17])
14 x4=int(data[3][0:2])+int(data[3][3:5])+int(data[3][6:8])+int(data[3][9:11])
15 x=(x1+x2+x3+x4)/17
16 print('{:.2f}'.format(x))

运行截图

实验任务8

源代码

 1 words_sensitive_list = ['张三', 'V字仇杀队', '杀']
 2 comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。',
 3 '电影<V字仇杀队>从豆瓣下架了',
 4 '娱乐至死']
 5 i=0
 6 while i<len(words_sensitive_list):
 7     t=0
 8     while t<len(words_sensitive_list):
 9         comments_list[i]=comments_list[i].replace(words_sensitive_list[t],'*'*len(words_sensitive_list[t]))
10         t+=1
11     i+=1
12 print('\n'.join(comments_list))

运行截图

实验任务9-1

源代码

 1 """
 2 家用电器销售系统
 3 v1.1
 4 """
 5 
 6 #欢迎信息
 7 print('欢迎使用家用电器销售系统!')
 8 
 9 #产品信息列表
10 print('产品和价格信息如下:')
11 print('*************************************************************************')
12 print('%-10s'%'编号','%-10s'%'名称','%-10s'%'品牌','%-10s'%'价格','%-10s'%'库存数量')
13 print('--------------------------------------------------------------------------')
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.00,'%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('--------------------------------------------------------------------------')
24 
25 #商品数据
26 product=[
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=len(product_id)-1
44 product=product[product_index]
45 
46 #计算金额
47 print('购买成功,您需要支付',product[3] * count,'元')
48 
49 #退出系统
50 print("谢谢您的光临,下次再见!")

运行截图

实验任务9-2

源代码

 

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

运行结果

 

标签:%-,10,int,列表,print,实验,字符串,10s,data
From: https://www.cnblogs.com/zyj-/p/17243171.html

相关文章

  • 由“交卷”功能引发的思考——对比两个字符串数组的差异
    最近在做一个答题系统,在交卷的时候需要判断客观题的答题情况客观题的题型有单选题、多选题、判断题其中判断题可以当做单选题处理,而单选题也可以当做标准答案长度为一的......
  • 实验二
    task2-1源码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......
  • FPGA实验—— 流水灯
    FPGA实验流水灯时钟频率与时钟周期计算\[f=\frac{1}{T}\]f是频率T是周期时钟周期的计算1s=1000ms=1000000us=1000000000ns对于100kHz=100000Hzf=1000000000/1......
  • 三级下拉列表
    问题:制作三级下拉列表数据源:省市列表  市县区列表  查询表 数据下拉列表+函数解决。【数据】》【下拉列表】》【从单元格选择下拉选项】》写入公式》【确......
  • PPPOE实验
    静态IPR1配置dialer-ruledialer-rule10ippermitinterfaceDialer1link-protocolpppppppaplocal-userhcippasswordcipher123ipaddressppp-negotiate......
  • go语言int64整型转字符串
    go语言中string(int)会把int当成UTF-8的Unicode值,转换成对应的字符,标准库strconv是专门用来实现基本数据类型和其字符串表示的相互转换。packagemainimport( "fmt" "s......
  • HJ29_字符串加解密_模拟
    思路:根据加解密规则,使字符串加解密后输出。这是初始理解,编码起来较麻烦。查看高赞题解后,学到一种新思路关于加解密:最佳方法是通过通过设计加解密表,代码比较简单,通过列表in......
  • 数组模拟双向列表 洛谷 P1160 队列安排
    题目描述一个学校里老师要将班上N个同学排成一列,同学被编号为1~N,他采取如下的方法:1.先将1号同学安排进队列,这时队列中只有他一个人;2.2~N号同学依次入列,编号为i的同学入列方式......
  • shell判断字符串结尾
    下面围绕“判断字符串是否以.txt结尾”展开。转变一下也同样适用于“判断字符串是否以.txt开头”。通用的方法#方法一、使用grep命令#!/bin/shstr="/path/to/foo.tx......
  • 实验2 字符串和列表
    实验任务1task.py实验源码:x='nbaFIFA'print(x.upper())print(x.lower())print(x.swapcase())print()x='abc'print(x.center(10,'*'))print(x.ljust(1......