Python求代码

[复制链接]
查看11 | 回复1 | 2021-1-27 05:08:49 | 显示全部楼层 |阅读模式
用Windows“记事本”创建一个文本文件,其中每行包含一段英文。试读出文件的全部内容,并判断:
(1)该文本文件共有多少行?
(2)文件中以大写字母P开头的有多少行?
(3)一行中包含字符最多的和包含字符最少的分别在第几行?
如:记事本内容为:
Alotofpeopletodayareanimalrightsadvocates.
Someofthemarevery
Passionateandevenquiteradicalabouttheissue.
Butothersarguethat“humanrights”willalways
takepriority.Infact,inmanyplaceseven
themostbasichumanrightsarenotadequately
Protected.Sowhyanimalrights?Whatdoyou
think?Andwhy?
运行结果为:该文本文件共有有8行
文件中以大写字母P开头的有2行
包含字符最多的是第1行
包含字符最少的是第8行


分 -->
回复

使用道具 举报

千问 | 2021-1-27 05:08:49 | 显示全部楼层
defline():
f=open("E:\\School\\大三\\Python\\第十一周\\second.txt",'r')
lines=f.readlines()
count=len(lines)
print("这文本文件共有:",count,"行")
defsearch():
f=open("2.txt",'r')
lines=f.readlines()
n=0
forlineinlines:
ifline[0]=='T':
n=n+1
print("文件中以大写字母T开头的有",n,"行")
defr():
f=open("English.txt",'r')
lines=f.readlines()
l=[]
foriinlines:
num=len(i.strip())
l.append(num)
print("一行中字符最多的在第",l.index(max(l))+1,"行")
print("一行中字符最少的在第",l.index(min(l))+1,"行")
line()
search()
r()
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行