【发布时间】:2022-10-25 22:50:34
【问题描述】:
当用户输入他们想看的单词时,它在哪一行。 所以代码会告诉你它在哪一行。
userAns = input("Enter english word: ")
print("I will try to find that word now!\n\n")
found = False
count = 0
with open("english3.txt", "r+") as f:
for line in f:
count += 1
if userAns == line:
print(f"I found it! in line {count}\n")
found = True
break
else:
continue
if not found:
print("I did not find it!\n")
print("I looked in a 1.8 MB file also")
print("I will have a larger file soon too!")
print("The code may get some thngs wrong")
该文件的第一行是“a”,所以当您输入“a”时。 它应该说“在第 1 行找到”或文件中的任何其他词
你能帮忙的话,我会很高兴
【问题讨论】:
-
if userAns in line:会是更好的方法
标签: python python-3.x