[英]How to run loop only once in python?
我是 python 和一般編碼的新手,我開發了一個小程序來檢查谷歌日歷中的重復預訂。 我的問題是,如果我選擇檢查 6 月 26 日,它將檢查 26 和 27。
我認為這是因為我在 range(1) 中做了一個 for i (所以它循環了兩次,(0,1))
我如何更改它以使其僅運行一次?
這是代碼:
for i in range(1):
startStrip = datetime.datetime.strptime(event_start, "%Y-%m-%dT%H:%M:%S")
endStrip = datetime.datetime.strptime(event_end, "%Y-%m-%dT%H:%M:%S")
dayOfWeek = startStrip + datetime.timedelta(days=i)
# les bons formats
currentStart = str(startStrip + datetime.timedelta(days=i)).replace(" ", "T")
currentEnd = str(endStrip + datetime.timedelta(days=i)).replace(" ", "T")
calendarEnd = str(endStrip + datetime.timedelta(days=i + 1)).replace(" ", "T")
events_result = service.events().list(calendarId='primary', timeMin=currentStart + "-00:00",
maxResults=30, timeMax=calendarEnd + "-00:00",
singleEvents=True, orderBy='startTime').execute()
events = events_result.get('items', [])
currentEmployees = []
for event in events:
currentEmployees.append(event['summary'])
#for i in range(1):
#event_done = False
if employee in currentEmployees:
event_done = False
event['summary'] = employee
#if employee not in currentEmployees:
#event_done = True
#else:
for event in events:
# if employee == event['summary']:
if str2datetime(currentStart) <= str2datetime(event['end']['dateTime'].split('+')[0]) and str2datetime(currentEnd) >= str2datetime(event['start']['dateTime'].split('+')[0]):
event_done = False
print(employee + ' est occupé')
break
else:
event_done = True
break
if employee not in currentEmployees:
event_done = True
if event_done:
option = show_message_box(QMessageBox.Critical,
"Confirmation",
"Voulez-vous bloquer cette plage horraire?"\
"L'employé : \"" + employee + "\" sera marqué comme indisponible en raison de : " + reason, \
"Nom de l'employé: " + employee + "\n" \
"Raison: " + reason + "\n" \
"À partir du : " + currentStart + "\n" \
"À ce jour " + currentEnd + "\n"
)
if option == QMessageBox.Yes:
event_done = True
else:
print("Événement ignoré!")
event_done = False
break
if event_done:
event = {
'summary': employee,
'location': location,
'description': reason,
'start': {
'dateTime': currentStart,
'timeZone': 'America/New_York',
},
'end': {
'dateTime': currentEnd,
'timeZone': 'America/New_York',
#},
#'attendees': [
# {'email': event_email},
#],
#'reminders': {
# 'useDefault': True,
},
}
register_event(service, event)
else:
second_message_box(QMessageBox.Critical,
"ATTENTION!",
"L'inspecteur " + employee + " est déjà occupé à ce moment-là.""\n" \
"Veuillez essayer une autre plage horraire.", QMessageBox.Ok)
你的第一個循環很好,它只會在我不知道你的需求邏輯時迭代,但我可以看到你的 calander_end 你正在用 +1 制作它,這實際上是提前一個日期我想你必須檢查並根據根據您的要求
首先,range() function 將值調用到“停止”輸入,這意味着你沒有得到 0 和 1,你只得到 i 的 0,其次,如果你只通過代碼一次去 go,你不要根本不需要循環? 如果你不想刪除它,你可以在循環塊的末尾放一個“中斷”,這會阻止循環再次運行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.