2017-08-13 168 views
0

首先,我想清楚说明我已经阅读并研究了有关此错误的一般信息......并且我已经阅读了一些其他在stackoverflow中的问题。但是,他们并没有帮助解决这个问题。 我写的程序应该会在有人有生日的时候给你一个通知(我在另一个txt文件中有日期)。但是,尽管当我运行该程序时,它仍然可以正常工作,直到它进入最后的if语句为止。然后它给出错误,List索引必须是整数或切片,而不是str。TypeError:列表索引必须是整数,而不是str(Python)

import time 
import os 

with open("file_path") as file: 
birthdays = file.readlines() 

while True: 
    import time 
    date = str((time.strftime("%d/%m"))) 
    for i in birthdays: 
     if date == birthdays[i]: 
      os.system("""osascript -e 'display notification "{}" with title "{}"'""".format("Someone Has A Birthday Today", "Birthday")) 

由于提前,
克里斯

+0

您需要'date == i',因为'i'不是索引。 –

回答

0

你所试图做的就是调用os.system一次为每个今天发生的生日。以下是你应该做的事情:

date = str((time.strftime("%d/%m"))) 
for i in birthdays: 
    if date == i: 

当你for - in循环或迭代一个列表,你在列表中的项目,该项目不索引。你不需要另外订阅。

相关问题