【问题标题】:Issue when going thru a .txt file looking for a word通过 .txt 文件查找单词时出现问题
【发布时间】: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


【解决方案1】:

你需要read()文件中的数据和split() (行) 然后遍历每个line 并检查您正在搜索的word 是否在那个line

userAns = input("Enter english word: ")
print("I will try to find that word now!

")
found = False
count = 0

with open("english3.txt", "r") as f:
    lines = f.read().split("
")
    for line in lines:
        count += 1
        if userAns in line:
            print(f"I found it! in line {count}
")
            found = True
            break
    if not found:
        print("I did not find it!
")

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

【讨论】:

    【解决方案2】:

    这个解决方案对我有用:

    userAns = input("Enter english word: ")
    print("I will try to find that word now!
    
    ")
    with open("english3.txt", "r+") as f:
        found = False
        count = 0
        for line in f:
            count +=1
            if userAns in line:
                print(f"I found it! in line {count}
    ")
                found = True
                break
            else:
                continue 
        if not found:
            print("I did not find it!
    ")
    

    【讨论】:

      【解决方案3】:

      伟大的尝试!这里有一些建议:

      1. 不需要在你的逻辑中继续

        您不需要 else continue 子句,因为这是 for 循环中的最后一条语句。逻辑将在没有继续的情况下再次进入 for 循环的顶部。

        1. 您可以使用 for / else 语句。

        当 for 循环因项目用完而终止时,使用 for 循环的 else 子句。当 break 被执行时,for 循环的 else: 子句不会。如果您使用 else 子句,您可以通过删除找到的变量来稍微简化您的逻辑。

        1. 使用“in”而不是“==”(取决于您的要求):

        == 仅当 userAns 与 line 完全相同时才有效。顺便说一句,我们还应该考虑新的线路。 line 变量可能有换行符 ( ) 最后,而用户输入 userAns 可能不会。一些需要考虑的事情,尽管它可能不是问题。简而言之,您应该考虑在本练习中使用 rstrip() 字符串函数从字符串中删除尾随空格。

        我将在对此回复的后续编辑中将其放在一起。但是,我想先给你一些指示。最重要的指针是上面的 3.,如果你没有匹配整行或者你被换行符打败,比较将不起作用 (' ')。

        这是上面建议的修改的代码:

        userAns = input("Enter english word: ")
        print("I will try to find that word now!
        
        ")
        count = 0
        
        with open("english3.txt", "r+") as f:
            for line in f:
              count += 1
              if userAns in line.rstrip():
                print(f"I found it! in line {count}
        ") 
                break
            else:
                print("I did not find it!
        ")
        
        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")
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-11
        • 2021-11-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-27
        相关资源
        最近更新 更多