首页 > 解决方案 > 如何正确使用 enumerate 和 .append 函数?

问题描述

我是编程新手,这是我第一次尝试自己做点什么。我正在尝试制作一个简单的程序来检测何时将某些内容写入 .txt 文件。现在我的“for”循环中的第一个“if”语句有效,所以我可以用它做点什么。但是当我想继续第二个时,即使满足 .txt 文件中的要求,也会出现问题并且它不会打印“success2”。我对 .append 和 enumerate 函数还不是很满意,所以我自己看不到问题所在。感谢任何可能提供帮助的人!

    from win32 import win32gui
    import win32ui
    import time



    def League_Running():
    #Checks if the client is running and returns True or False
    if win32ui.FindWindow(None, "League of Legends (TM) Client"):
        return True
    else:
        print("not found")
        return False


   firstused = False 
   secondused = False

   while League_Running():
       content = []
       linenumber = {4}

       for i, note in enumerate(open(r'C:\Riot Games\League of Legends\MyNotes.txt', "r")): #Opens the .txt file
           if i in linenumber:
               if linenumber == {4} and linenumber!= None and not firstused:
                   content.append(note) #adds the line in the .txt file to the content list
                   if "test" in str(content):  #checks for a specific line in the content list
                       print("success")
                       firstused = True #so it doesnt get printed out 100x every second, will add time.sleep() later
                       linenumber = {5}
                   content.remove(note) #not sure if this is necessary?

                if linenumber == {5} and linenumber!= None and not secondused:
                    content.append(note) #pretty sure this is where it breaks

                    if "hello" in str(content):
                        print("success2")
                        secondused = True
                        linenumber = {6}

.txt 文件的内容:

#

2020-05-16_14-29-53_

#

测试

你好

标签: python

解决方案


推荐阅读