关于条件流语句的简单问题
Simple Question About Conditional Flow Statements
我有一段代码可以读取 Outlook 电子邮件并根据特定条件搜索电子邮件,但是,问题是一旦找到所有满足条件的电子邮件,代码就不会停止。它只是保持 运行,即使它 returns 仅此而已。
这是我目前拥有的:
我试过使用 break 语句和多个 if 语句以及 elif。但我似乎无法让它发挥作用。
import win32com.client
from datetime import date, timedelta
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(18).Folders.Item("Riscunit")
messages = inbox.Items
date = date.today() - timedelta(days=1)
subject = "Catalyst"
for message in messages:
if subject in message.subject and date == message.senton.date():
print(message.sender)
print(message.senton.date())
print(message.senton.time())
print(message.body)
elif subject != message.subject and date != message.senton.date:
break
我想要代码检索相关电子邮件然后停止 运行。我是 Python 的新手,如有任何帮助,我们将不胜感激。
可能是因为使用elif
而不是else
的逻辑。
试试这个:
import win32com.client
from datetime import date, timedelta
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(18).Folders.Item("Riscunit")
messages = inbox.Items
date = date.today() - timedelta(days=1)
subject = "Catalyst"
for message in messages:
if subject in message.subject and date == message.senton.date():
print(message.sender)
print(message.senton.date())
print(message.senton.time())
print(message.body)
else:
subject != message.subject and date != message.senton.date
break
-
elif
是不必要的,因为它与 if
相反
- 如果您确实需要在
if
为 false 时做某事,那么 else
会更合适——尽管您可能不需要此处的 else
bag = ['pizza', 'ziplocks','bananas', 'milk glass', 'post-its','spray']
foods = {1:'pizza', 2:'bananas', 3:'milk'}
# check shopping bag for food
for item in bag:
for key in foods:
if foods[key] in item:
print('eat ' + foods[key]);
break
else:
print('put ' + item + ' away');
print("done");
这是另一个在不满足 if 条件时执行某些操作的示例:
messages = [{'subject': 'shopping list', 'date': '05/10/2019', 'body': 'milk'},
{'subject': 'shopping list', 'date': '05/10/2019', 'body': 'pizza'},
{'subject': 'holiday', 'date': '12/10/2015', 'body': 'need vacation soon'},
{'subject': 'shopping list', 'date': '12/10/2015', 'body': 'we need potatoes'}]
date = '05/10/2019'
subject = "shopping list"
for item in messages:
for key in item:
if subject in item['subject'] and item['date'] == date:
print('buy ' + item['body']);
break
else:
print('archive \"' + item['body'] + '\" email');
break
print("done");
您可能需要限制测试的邮件数量以查看它们是否符合您的要求:
max = 42
for count, message in enumerate(messages):
if count > max:
break
if subject in message.subject and date == message.senton.date():
collect(message) # Do something with this message (print or append to list)
我有一段代码可以读取 Outlook 电子邮件并根据特定条件搜索电子邮件,但是,问题是一旦找到所有满足条件的电子邮件,代码就不会停止。它只是保持 运行,即使它 returns 仅此而已。
这是我目前拥有的:
我试过使用 break 语句和多个 if 语句以及 elif。但我似乎无法让它发挥作用。
import win32com.client
from datetime import date, timedelta
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(18).Folders.Item("Riscunit")
messages = inbox.Items
date = date.today() - timedelta(days=1)
subject = "Catalyst"
for message in messages:
if subject in message.subject and date == message.senton.date():
print(message.sender)
print(message.senton.date())
print(message.senton.time())
print(message.body)
elif subject != message.subject and date != message.senton.date:
break
我想要代码检索相关电子邮件然后停止 运行。我是 Python 的新手,如有任何帮助,我们将不胜感激。
可能是因为使用elif
而不是else
的逻辑。
试试这个:
import win32com.client
from datetime import date, timedelta
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(18).Folders.Item("Riscunit")
messages = inbox.Items
date = date.today() - timedelta(days=1)
subject = "Catalyst"
for message in messages:
if subject in message.subject and date == message.senton.date():
print(message.sender)
print(message.senton.date())
print(message.senton.time())
print(message.body)
else:
subject != message.subject and date != message.senton.date
break
-
elif
是不必要的,因为它与if
相反
- 如果您确实需要在
if
为 false 时做某事,那么else
会更合适——尽管您可能不需要此处的else
bag = ['pizza', 'ziplocks','bananas', 'milk glass', 'post-its','spray']
foods = {1:'pizza', 2:'bananas', 3:'milk'}
# check shopping bag for food
for item in bag:
for key in foods:
if foods[key] in item:
print('eat ' + foods[key]);
break
else:
print('put ' + item + ' away');
print("done");
这是另一个在不满足 if 条件时执行某些操作的示例:
messages = [{'subject': 'shopping list', 'date': '05/10/2019', 'body': 'milk'},
{'subject': 'shopping list', 'date': '05/10/2019', 'body': 'pizza'},
{'subject': 'holiday', 'date': '12/10/2015', 'body': 'need vacation soon'},
{'subject': 'shopping list', 'date': '12/10/2015', 'body': 'we need potatoes'}]
date = '05/10/2019'
subject = "shopping list"
for item in messages:
for key in item:
if subject in item['subject'] and item['date'] == date:
print('buy ' + item['body']);
break
else:
print('archive \"' + item['body'] + '\" email');
break
print("done");
您可能需要限制测试的邮件数量以查看它们是否符合您的要求:
max = 42
for count, message in enumerate(messages):
if count > max:
break
if subject in message.subject and date == message.senton.date():
collect(message) # Do something with this message (print or append to list)