今天练习了一个简单的项目,后面有我自己在网上找的一段文章用作练习。
0004题–任一个英文的纯文本文件,统计其中的单词出现的个数.
import sys
x=input("请输入你查找的单词")
txt1=open(r'D:\python练习内容\learning1.txt')
txt2=txt1.read()
txt_list=txt2.split(' ')
a=0
i=0
for i in range(len(txt_list)):
if txt_list[i] == x:
a += 1
print(x,"在文章中出现的次数为:")
print(a)
我所使用的文章是这一段:
Hooray! It’s snowing! It’s time to make a snowman.James runs out. He makes a big pile of snow. He puts a big snowball on top. He adds a scarf and a hat. He adds an orange for the nose. He adds coal for the eyes and buttons.In the evening, James opens the door. What does he see? The snowman is moving! James invites him in. The snowman has never been inside a house. He says hello to the cat. He plays with paper towels.A moment later, the snowman takes James’s hand and goes out.They go up up up into the air! They are flying! What a wonderful night!The next morning, James jumps out of bed. He runs to the door.He wants to thank the snowman. But he’s gone.
来源:CSDN
作者:Lesliezhangzss
链接:https://blog.csdn.net/Lesliezhangzss/article/details/103993808