如何停止输入冲突或过期的约会?

问题描述 投票:0回答:1

当前时间在代码开始时使用...定义

from datetime import datetime
now = datetime.now()
auformat = now.strftime("%d-%m-%Y %H:%M:%S")
# print(auformat)

我需要停止输入并发约会并停止插入过去的约会。这是我的代码,但我不知道该把它放在哪里,因为它需要位于下面的“addRecord”代码中的“日期、开始、结束”之后。

def isConcurrentAppointment(date, start, end):
    if isConcurrentAppointment(date, >=appt.start, >=appt.end): # greater than or equal to
        print("Cuncurrent appointment exists. Try again.")
        continue
    elif appt.start <=now: # OR <=current.time: - Not sure what to use
        print("Invalid entry. Appointment must be in the future. Try again!")
        continue

输入(addRecord)代码

def addRecord(schedule):
    date = input("Enter a date (DD/MM/YYYY): ")
    date = date.strip()
    start = input("Enter a start time (HH): ")
    start = start.strip()
    end = input("Enter an end time (HH): ")
    end = end.strip()
    '''
    I think the above code should slot in here somehow!
    '''
    description = input("Enter a description (Max. 25 characters with spaces): ")
    venue = input("Enter a venue  (Max. 25 characters with spaces): ")
    priority = input("Enter a priority (High, Medium or Low): ")
    priority = priority.strip() # if isConcurrentAppointment statement goes maybe below here.
    appointment = Appointment(date,start,end,description,venue,priority)
    schedule.append(appointment)
    print("Appointment added.")

如前所述,我创建了要插入的代码,但无法确定插入位置,因为它有一个“def”语句。也许我需要以某种方式失去“def”行?

def isConcurrentAppointment(date, start, end):
    if isConcurrentAppointment(date, >=appt.start, >=appt.end): # greater than or equal to
        print("Cuncurrent appointment exists. Try again.")
        continue
    elif appt.start <=now: # OR <=current.time: - Not sure what to use
        print("Invalid entry. Appointment must be in the future. Try again!")
        continue

我现在已经尝试了以下方法

def addRecord(schedule):
    date = input("Enter a date (DD/MM/YYYY): ")
    date = date.strip()
    start = input("Enter a start time (HH): ")
    start = start.strip()
    end = input("Enter an end time (HH): ")
    end = end.strip()
    def isConcurrentAppointment(date, start, end):
        if isConcurrentAppointment(date, appt.start, appt.end):
            print("Cuncurrent appointment exists. Try again.")
            continue
        elif appt.start <=now:
            print("Invalid entry. Appointment must be in the future. Try again!")
            continue
    description = input("Enter a description (Max. 25 characters with spaces): ")
    venue = input("Enter a venue  (Max. 25 characters with spaces): ")
    priority = input("Enter a priority (High, Medium or Low): ")
    priority = priority.strip() # if isConcurrentAppointment statement goes maybe below here.
    appointment = Appointment(date,start,end,description,venue,priority)
    schedule.append(appointment)
    print("Appointment added.")

错误现在显示“SyntaxError:‘继续’外部循环”。如果我删除 continue 语句,程序如何知道重新提出问题。

一旦我删除了“继续”语句,程序就会运行,但允许输入冲突的约会。上面的代码不应该阻止这个吗?

我很确定这与我不能在这样的代码中间放置 def 语句有关,但需要 def 语句来验证 isConcurrentAppointment 字符串。

本质上代码仍然无法工作。不过还是谢谢大家的帮助。

我现在已经尝试了以下...

def isConcurrentAppointment(date, start, end):
    if isConcurrentAppointment(date, appt.start, appt.end):
        print("Cuncurrent appointment exists. Try again.")
    elif appt.start <=now:
        print("Invalid entry. Appointment must be in the future. Try again!")
        
def addRecord(schedule):
    date = input("Enter a date (DD/MM/YYYY): ")
    date = date.strip()
    start = input("Enter a start time (HH): ")
    start = start.strip()
    end = input("Enter an end time (HH): ")
    end = end.strip()
    description = input("Enter a description (Max. 25 characters with spaces): ")
    venue = input("Enter a venue  (Max. 25 characters with spaces): ")
    priority = input("Enter a priority (High, Medium or Low): ")
    priority = priority.strip() # if isConcurrentAppointment statement goes maybe below here.
    appointment = Appointment(date,start,end,description,venue,priority)
    schedule.append(appointment)
    print("Appointment added.")

...但还是不行。我可以输入系统中已有的并发约会,也可以输入系统中过去日期的约会。

代码仍然无法正常工作,但又再次出现。

我是一名会计师和 HTML/CSS/JavaScript 网站开发人员(自学成才,如果我自己这么说的话,那也相当不错),正在参加在线 Python 训练营课程,将 Python 添加到我已经拥有的 Web 开发技能中,但 Python 实在是太丰富了与我已经知道的任何事情都不同。我需要帮助理解为什么我的代码是错误的,而不仅仅是被告知基本原理并自己弄清楚。如果人们不说我的代码哪里错了以及为什么,我该如何学习。

尽管如此,还是非常感谢大家的尝试。我也会继续努力的。

python concurrency future appointment
1个回答
0
投票

一般来说,您可以通过以下方法确定所请求的预约是否与任何其他现有预约重叠。

  1. 循环每个现有约会。
  2. 如果请求的约会开始时间介于现有约会的开始和结束时间之间,或者请求的约会的结束时间介于现有约会的开始和结束时间之间,则两个约会重叠。
  3. 如果您没有发现任何此类重叠,则所请求的预约不会与任何其他现有预约重叠。
© www.soinside.com 2019 - 2024. All rights reserved.