满足条件时如何停止代码

时间:2020-06-14 04:57:02

标签: python if-statement

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次。

1 个答案:

答案 0 :(得分:0)

您可以使用关键字“ 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

此继续操作将在运行时阻止循环结束

for 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

希望这会有所帮助