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

实验2 字符串和列表

时间:2023-03-22 23:34:37浏览次数:34  
标签:%- 10 list len 列表 print 实验 字符串 10s

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

x=[1,9,8,4,2,0,49]
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))

task3

 

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()

t=[]
i=0
while i < len(name_list):
    t.append(name_list[i].title())
    i+=1
print('\n'.join(t))

 

task4

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

 

 

task5

x="""
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!
"""
print('行数:',x.count('\n'))
alpha=0
b=0
for i in x:
    if i.isalpha():
        alpha+=1
    else:
        b+=1
print('字符数:',alpha+b)
word=len(x.split())
print('单词数:',word)
print('空格数:',x.count(' '))

task6

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

 

task7

data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100']
title='Remote Interpreter Reinitialized'
print(title.center(40,'*'))
data=','.join(data)
import re
data_list = re.findall(r"\d+",data)
data_list=list(map(int,data_list))
i=0
sum=0

while i< len(data_list):
    sum+=data_list[i]
    i+=1
av=sum/(len(data_list))
import decimal
decimal.getcontext().rounding="ROUND_HALF_UP"
av=decimal.Decimal(av).quantize(decimal.Decimal("0.00"))

print(av)

 

task8

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

 

task9_1

print('欢迎使用家用电器销售系统!')
print('产品和价格信息如下')
print('***********************************************************')
print('%-10s'%'编号','%-10s'%'名称','%-10s'%'品牌','%-10s'%'价格','%-10s'%'库存数量')
print('-----------------------------------------------------------')
print('%-10s'%'0001','%-10s'%'电视机','%-10s'%'海尔','%10.2f'%5900.00,'%10d'%20)
print('%-10s'%'0002','%-10s'%'冰箱','%-10s'%'西门子','%10.2f'%6998.00,'%10d'%15)
print('%-10s'%'0003','%-10s'%'洗衣机','%-10s'%'小天鹅','%10.2f'%1900.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('-----------------------------------------------------------')
pro_id=input('请输入您要购买的产品编号:')
count=int(input('请输入您要购买的产品数量:'))
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.00,33],
          ['0008','投影仪','松下',1250.00,12],
          ['0009','吸尘器','飞利浦',999.00,9]]
product_index=len(pro_id)-1
product=products[product_index]

print('购买成功,您需要支付',product[3]*count,'元')
print('谢谢您的光临,下次再见!')

 

task9_2

print('欢迎使用家用电器销售系统!')
print('产品和价格信息如下')
print('***********************************************************')
print('%-10s'%'编号','%-10s'%'名称','%-10s'%'品牌','%-10s'%'价格','%-10s'%'库存数量')
print('-----------------------------------------------------------')
pro  =    [['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.00,33],
          ['0008','投影仪','松下',1250.00,12],
          ['0009','吸尘器','飞利浦',999.00,9]]
i=0
t=0
while i < len(pro):
    print('{:<10}'.format(pro[i][0]), '{:<10}'.format(pro[i][1]), '{:<10}'.format(pro[i][2]), '{:<10.2f}'.format(pro[i][3]),
          '{:>10}'.format(pro[i][4]))
    i+=1

print('-----------------------------------------------------------')
pro_id=input('请输入您要购买的产品编号:')
count=int(input('请输入您要购买的产品数量:'))
product_index=len(pro_id)-1
product=pro[product_index]

print('购买成功,您需要支付',product[3]*count,'元')
print('谢谢您的光临,下次再见!')

 

 

 

 

标签:%-,10,list,len,列表,print,实验,字符串,10s
From: https://www.cnblogs.com/gkyi1/p/17245892.html

相关文章

  • 实验四
    实验报告实验名称IPTABLES基本操作实验实验地点S408实验日期2023.3.9成绩实验目的了解Iptables的原理和基本命令实验原理1.IptablesIptables是用来设置、维护和检查Li......
  • 实验2
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;sr......
  • 实验2
    task1(1)line18代码实现的功能是:一个586到781间的随机数(2)该程序的功能为随机生成586到781之间的"真·随机数"task2//.2.2.c.cpp:定义控制台应用程序的入......
  • 实验2
    task1编程代码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;s......
  • 实验二
    实验任务1源码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;......
  • 实验二
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;srand(time......
  • 实验2 字符串和列表
    一、实验结论:1、实验任务1:task1.py程序源码:1#字符串的基础操作2#课堂上没有演示的一些方法34x='nbaFIFA'5print(x.upper())#字符串转......
  • 实验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,'*'......
  • 实验2
    2.1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN6#defineR1586#defineR2701intmain(){intnumber;inti;srand(time(0));for(i=0;......
  • 实验2
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;srand(ti......