我正在尝试创建一个程序,它计算行数,计算一个特定单词,然后告诉你它在哪一行.现在我做了前两个,但我不知道如何找到这个词的哪一行.有什么建议 ?这是我目前的代码:
name = input('Enter name of the text file: ')+'.txt'
file = open(name,'r')
myDict = {}
linenum = 0
for line in file:
line = line.strip()
line = line.lower()
line = line.split()
linenum += 1
print(linenum-1)
count = 0
word = input('enter the word you want us to count, and will tell you on which line')
#find occurence of a word
with open(name, 'r') as inF:
for line in inF:
if word in line:
count += 1
#find on …Run Code Online (Sandbox Code Playgroud) 我有以下伪代码,顺序搜索伪代码,我试图理解返回-1的意思.我们为什么要返回-1,有人可以解释一下.
A[n] <-- K
i <-- 0
while A[i] != K do
i = i + 1
if(i<n)
return i;
else
return -1; //What is this mean ?
Run Code Online (Sandbox Code Playgroud)