如何使我的代码在满足条件时停止

2024-06-26 14:16:33 发布

您现在位置:Python中文网/ 问答频道 /正文

for i in range(len(employeesChosen)):
    info = get_employee_info(employeesChosen[i])
    event_done=False
    if employeesChosen[i] not in currentEmployees and check_employee_availability(service,employeesChosen[i],currentStart,currentEnd,currentStart,calendarEnd):
        event_done=True
    else:
        for event in events:
            if employeesChosen[i] == event['summary']:

                if str2datetime(currentStart) == str2datetime(event['start']['dateTime'].split('+')[0]) and str2datetime(currentEnd) == str2datetime(event['end']['dateTime'].split('+')[0]):
                    event_done = False

                if str2datetime(currentStart) < str2datetime(event['end']['dateTime'].split('+')[0]):  #rdv avant un qui est deja set
                    event_info = {'location': get_company_location(event['description']),'datetime': event['end']['dateTime'].split('+')[0]}
                    start_first_event = {'location': get_company_location(event['description']),'datetime': event['start']['dateTime'].split('+')[0]}
                    event_fabricant_info = {'location': get_company_location(event_fabricant),'datetime': currentStart}
                    end_second_event = {'location': get_company_location(event_fabricant),'datetime': currentEnd}
                    if check_event_possibility_if_before(event_info, event_fabricant_info, end_second_event, start_first_event, info[0]):
                        event_done = True
                    else:
                        event_done = False

                if str2datetime(currentStart) > str2datetime(event['end']['dateTime'].split('+')[0]) or str2datetime(currentEnd): #rdv apres un qui est deja set
                    event_info={'location': get_company_location(event['description']), 'datetime': event['end']['dateTime'].split('+')[0]}
                    start_first_event ={'location': get_company_location(event['description']), 'datetime': event['start']['dateTime'].split('+')[0]}
                    event_fabricant_info = {'location': get_company_location(event_fabricant), 'datetime': currentStart}
                    end_second_event = {'location': get_company_location(event_fabricant), 'datetime': currentEnd}
                    if check_event_possibility(event_info, event_fabricant_info, end_second_event,start_first_event, info[0]):
                        event_done=True
                    else:
                        event_done=False

我有一些问题,希望你们能帮我解决。我是python新手,所以如果我的问题已经被回答了几次,请原谅

我想知道,在声明下面

if employeesChosen[i] == event['summary']:

,如何使每个if只运行一次?现在,即使第一个条件是False,它也会一直运行到最后。我希望代码只锁定其中一个if条件。for将循环3次


Tags: infoeventgetdatetimeiflocationstartcompany
1条回答
网友
1楼 · 发布于 2024-06-26 14:16:33

您可以使用关键字“continue”来打破循环;或者,您可以使用关键字elif/else

对于“继续”:

for event in events:
    if employeesChosen[i] == event['summary']:
         if ....(conditions):
              event_done = False
              continue // if you want to stop here

         if ...(conditions):
              continue
         if ...(conditions):
              continue

此continue将停止循环在运行时完成

对于if-else循环:

for event in events:
    if ...(conditions):
    elif ...(conditions): // will run this if the above condition does not match, will run code after else, if no return, "continue" or "break" keyword
    else: // will run this if none of the above matches, will run code after else, if no return    

上面的代码演示了如何使用elif和else“跳过”ifs

希望这有帮助

相关问题 更多 >