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

实验二:字符串和列表

时间:2023-03-27 16:46:03浏览次数:29  
标签:%- 10 format 列表 print 实验 10s 字符串 products

          试验任务一

 task1

    代码:

x = 'nba FIFA'
print(x.upper())
print(x.lower())
print(x.swapcase())
print()
x = 'abc'
print(x.center(10, '*'))
print(x.ljust(10, '*'))
print(x.rjust(10, '*'))
print()
x = '123'
print(x.zfill(10))
x = 123
print(str(x).zfill(10))
print()
x = 'phone_number'
print(x.isidentifier())
x = '222test'
print(x.isidentifier())
print()
x = ' '
print(x.isspace())
x = '\n'
print(x.isspace())
print()
x = 'python is fun'
table = x.maketrans('thon', '1234')
print(x.translate(table))

    运行结果:

                                                                                                            试验任务二

 

task2

     代码:

x = [5, 11, 9, 7, 42]
print('整数输出1: ', end = '')
i = 0
while i < len(x):
 print(x[i], end = ' ')
 i += 1
print('\n整数输出2: ', end = '')
i = 0
while i < len(x):
 print(f'{x[i]:02d}', end = '-')
 i += 1
print('\n整数输出3: ', end = '')
i = 0
while i < len(x) - 1:
 print(f'{x[i]:02d}', end = '-')
 i += 1
print(f'{x[-1]:02d}')
print('\n字符输出1: ', end = '')
y1 = []
i = 0
while i < len(x):
 y1.append(str(x[i]))
 i += 1
print('-'.join(y1))
print('字符输出2: ', end = '')
y2 = []
i = 0
while i < len(x):
 y2.append( str(x[i]).zfill(2) )
 i += 1
print('-'.join(y2))

运行结果:

当line3的x换[1, 9, 8, 4, 2, 0, 49]
运行结果:

                                                                                                                                         实验3

task3

方法1

name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan',
'cocteau twins']
i = 0
while i < len(name_list):
 print(name_list[i].title())
 i += 1
print()

结果

方法2

name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan',
'cocteau twins']
t = []
i = 0
while i < len(name_list):
 t.append(name_list[i].title())
 i += 1
print('\n'.join(t))

结果

                                                                                                                                        实验4

task4

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

 

结果

 

                                                                                                                                                    实验5

task5

file = '''The Zen of Python, by Tim Peters
 
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!'''
line = file.count('\n')
character = len(str.strip(file))
word = len(file.split())
space = file.count(' ')
print(f'行数:{line}')
print(f'单词数:{word}')
print(f'字符数:{character}')
print(f'空格数:{space}')

运行结果

                                                                                                                                     任务6

代码

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

实验结果

                                                                                                                      试验任务7

代码

date = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100']
x = (99 + 81 + 75 + 30 + 42 + 90 + 87 + 69 + 50 + 96 + 77 + 89 + 93 + 82 + 99 + 78 + 100)/17
print(f'x = {x:.2f}')

结果

                                                                                                                                                   试验任务8

代码

words_sensitive_list = ['张三', 'V字仇杀队', '杀']
comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。','电影<V字仇杀队>从豆瓣下架了','娱乐至死']
for i in words_sensitive_list:
    for j in range(0,len(comments_list)):
        if i in comments_list[j]:
            comments_list[j] = comments_list[j].replace(i,'*'*len(i))
for line in comments_list:
    print(line)

结果

                                                                                                                                              试验任务9.1

代码

"""
家用电器销售系统
v1.1
"""
#欢迎信息
print('欢迎使用家用电器销售系统!')

#产品信息列表
print('产品和价格信息如下:')
print('***********************************************************')
print('%-10s'%'编号','%-10s'%'名称','%-10s'%'品牌','%-10s'%'价格','%-10s'%'库存数量')
print('-----------------------------------------------------------')
print('%-10s'%'0001','%-10s'%'电视机','%-10s'%'海尔','%10.2f'%5999.00,'%10d'%20)
print('%-10s'%'0002','%-10s'%'冰箱','%-10s'%'西门子','%10.2f'%6998.00,'%10d'%15)
print('%-10s'%'0003','%-10s'%'洗衣机','%-10s'%'小天鹅','%10.2f'%1999.00,'%10d'%10)
print('%-10s'%'0004','%-10s'%'空调','%-10s'%'格力','%10.2f'%3900.00,'%10d'%0)
print('%-10s'%'0005','%-10s'%'热水器','%-10s'%'美的','%10.2f'%688.00,'%10d'%30)
print('%-10s'%'0006','%-10s'%'笔记本','%-10s'%'联想','%10.2f'%5699.00,'%10d'%10)
print('%-10s'%'0007','%-10s'%'微波炉','%-10s'%'苏泊尔','%10.2f'%480.50,'%10d'%33)
print('%-10s'%'0008','%-10s'%'投影仪','%-10s'%'松下','%10.2f'%1250.00,'%10d'%12)
print('%-10s'%'0009','%-10s'%'吸尘器','%-10s'%'飞利浦','%10.2f'%999.00,'%10d'%9)
print('-----------------------------------------------------------')

#商品数据
products = [
['0001','电视机','海尔',5999.00,20],
['0002','冰箱','西门子',6998.00,15],
['0003','洗衣机','小天鹅',1999.00,10],
['0004','空调','格力',3900.00,0],
['0005','热水器','美的',688.00,30],
['0006','笔记本','联想',5699.00,10],
['0007','微波炉','苏泊尔',480.50,33],
['0008','投影仪','松下',1250.00,12],
['0009','吸尘器','飞利浦',999.00,9]
]

#用户输入信息
product_id = input('请输入您要购买的产品编号:')
count = int(input('请输入您要购买的产品数量:'))

#获取编号对应商品的信息
product_index = int(product_id) - 1
product = products[product_index]

#计算金额
print('购买成功,您需要支付',product[3]*count,'元')

#退出系统
print('谢谢您的光临,下次再见!')

结果

                                                                                                                   试验任务9.2

代码

"""
家用电器销售系统
v1.1
"""
#欢迎信息
print('欢迎使用家用电器销售系统!')

#商品数据
products = [
['0001','电视机','海尔',5999.00,20],
['0002','冰箱','西门子',6998.00,15],
['0003','洗衣机','小天鹅',1999.00,10],
['0004','空调','格力',3900.00,0],
['0005','热水器','美的',688.00,30],
['0006','笔记本','联想',5699.00,10],
['0007','微波炉','苏泊尔',480.50,33],
['0008','投影仪','松下',1250.00,12],
['0009','吸尘器','飞利浦',999.00,9]
]

#产品信息列表
print('产品和价格信息如下:')
print('{:*^55}'.format('*'))
print('{:10}'.format('编号'),'{:10}'.format('名称'),'{:10}'.format('品牌'),'{:10}'.format('价格'),'{:10}'.format('库存数量'))
print('{:-^55}'.format('-'))
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]))
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]))
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]))
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]))
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]))
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]))
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]))
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]))
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]))
print('{:-^55}'.format('-'))

#用户输入信息
product_id = input('请输入您要购买的产品编号:')
count = int(input('请输入您要购买的产品数量:'))

#获取编号对应商品的信息
product_index = int(product_id) - 1
product = products[product_index]

#计算金额
print('购买成功,您需要支付',product[3]*count,'元')

#退出系统
print('谢谢您的光临,下次再见!')

结果

 

标签:%-,10,format,列表,print,实验,10s,字符串,products
From: https://www.cnblogs.com/wang6666/p/17243189.html

相关文章

  • 实验二 字符串和列表
    实验任务1task1.py1#字符串的基础操作2#课堂上没有演示的一些方法34x='nbaFIFA'5print(x.upper())6print(x.lower())7print(x.swapcase())8......
  • C语言—字符函数和字符串函数解析及其模拟实现
    目录一、求字符串的长度1、strlen()-字符串长度二、长度不受限制的字符串函数1、strcpy()-字符串拷贝2、strcat()-字符串追加3、strcmp()-字符串比较三、长度受限制的字符串函数1、s......
  • Android 视频列表(RecyclerView)实现自动播放
    最近公司出了新的需求,想让视频列表滑到哪里,哪里就自动播放.于是乎,深海开始动手了:第一步,先让视频可以手动点击播放这个简单:就往ViewHolder里放一个视频播放器就可以了......
  • Redis 列表(List)
    Redis列表(List)Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)一个列表最多可以包含232-1个元素(4294967295,......
  • 第三章 列表简介
    列表是什么#用[]来表示列表,列表中的元素用,隔开list=['element_one','element_two']#访问列表元素程序员的数学中开头的数字不是1,而是0print(list[0])#打印li......
  • 第四章 操作列表
    遍历整个列表#for循环编写for循环时,选择有意义的列表名称大有帮助#如下面的mgicians表示魔术师列表一般性列表,像下面这样编写for循环的第一行代码是不错的选择:ma......
  • c#动态执行字符串脚本(优化版)
    像javascript中有eval()来执行动态代码,c#中是没有的,于是自己动手丰衣足食,先来代码1usingSystem;2usingSystem.Data;3usingSystem.Configuration;4us......
  • Python list列表添加元素
    Pythonappend()方法添加元素append()方法用于在列表的末尾追加元素,该方法的语法格式如下:listname.append(obj)其中,listname表示要添加元素的列表;obj表示到添加到列......
  • 【DP】LeetCode 剑指 Offer 46. 把数字翻译成字符串
    题目链接剑指Offer46.把数字翻译成字符串思路这个问题与dp中的经典问题“跳台阶”问题十分类似,在跳台阶问题中我们是选择跳一个台阶或者两个台阶,而在这个问题中我......
  • 前端传递Base64字符串,后端转流存入OSS
    工具类publicstaticBufferedInputStreambase64Convert(Stringbase64){//解码base64=base64.split(",")[1];try{byte[]......