2017-08-13 167 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循環或迭代一個列表,你在列表中的項目,該項目不索引。你不需要另外訂閱。

相關問題