簡體   English   中英

滿足條件時如何使我的代碼停止

[英]How to make my code stops when conditions are met

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

您可以使用關鍵字“繼續”來打破循環; 或者,您可以使用關鍵字 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 將在運行時停止循環完成

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

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM