Python 它不允许我比较这个函数第二部分的日期
我需要你能够在这个功能比较两个日期。这是为了确保用户输入了有效日期。出于某种原因,它不允许我比较日期 我已经尝试过使用.date()和切换日期,以及使用不同的运算符Python 它不允许我比较这个函数第二部分的日期,python,python-3.x,csv,Python,Python 3.x,Csv,我需要你能够在这个功能比较两个日期。这是为了确保用户输入了有效日期。出于某种原因,它不允许我比较日期 我已经尝试过使用.date()和切换日期,以及使用不同的运算符 def DateToBook(): 全局日期开始 全局日期结束 今天=日期。今天() 打印(“今天的日期是:”,Today.strftime(“%d/%m/%Y”)) 尽管如此: #这是我们希望输入数据的日期格式 日期格式=“%d/%m/%Y” #允许用户输入他们想要预订房间的日期 datestart=输入(“您希望预订房间的日期是
def DateToBook():
全局日期开始
全局日期结束
今天=日期。今天()
打印(“今天的日期是:”,Today.strftime(“%d/%m/%Y”))
尽管如此:
#这是我们希望输入数据的日期格式
日期格式=“%d/%m/%Y”
#允许用户输入他们想要预订房间的日期
datestart=输入(“您希望预订房间的日期是什么?键入日期dd/mm/yyyy:”)
尝试:
datestart=dt.datetime.strtime(datestart,dateformat)#检查格式是否匹配
除值错误外:
打印(“格式错误”)#告诉用户格式错误
#让他们再试一次
其他:
如果datestart.date()<今天:
打印(“无效日期”)
其他:
datestart=datestart.strftime(“%d/%m/%Y”)#这将去掉不必要的部分
break#如果未发现错误,则继续执行程序
尽管如此:
#允许用户输入预订结束的日期
dateend=输入(“您希望预订结束的日期?键入数据dd/mm/yyyy:”)
尝试:
dateend=dt.datetime.strtime(dateend,dateformat)
除值错误外:
打印(“格式不正确”)
其他:
如果datestart.date()>dateend:#抛出错误
打印(“输入有效日期”)
其他:
dateend=dateend.strftime(“%d/%m/%Y”)#这将去掉不必要的部分
打破
您的第一个while循环通过将datestart
转换为字符串来结束。这使得它无法在第二个while循环中应用.date()
方法。尝试将新字符串版本的datestart
命名为不同的名称
def DateToBook():
global datestart
global dateend
today = date.today()
print("Today's date is: ", today.strftime('%d/%m/%Y'))
while True:
# This is the date format in which we want the data entered in
dateformat = '%d/%m/%Y'
# Allow the user to input what date they would like to book the room
datestart = input("What date would you like the room to be booked? Type Date dd/mm/yyyy: ")
try:
datestart = dt.datetime.strptime(datestart, dateformat) # Checks to see if the format matches
except ValueError:
print("Incorrect format") # Tells the user that the format is wrong
# Makes them try again
else:
if datestart.date() < today:
print("Invalid date")
else:
str_datestart = datestart.strftime('%d/%m/%Y') # This takes off the unnecessary parts
break # Carries on the program if no errors are found
while True:
# Allow the user to enter the date the booking will end
dateend = input("What date would you like the booking to end? Type Data dd/mm/yyyy: ")
try:
dateend = dt.datetime.strptime(dateend, dateformat)
except ValueError:
print("Incorrect format")
else:
if datestart.date() > dateend: # Throws error
print("Enter a valid date")
else:
str_dateend = dateend.strftime('%d/%m/%Y') # This takes off the unnecessary parts
break
def DateToBook():
全局日期开始
全局日期结束
今天=日期。今天()
打印(“今天的日期是:”,Today.strftime(“%d/%m/%Y”))
尽管如此:
#这是我们希望输入数据的日期格式
日期格式=“%d/%m/%Y”
#允许用户输入他们想要预订房间的日期
datestart=输入(“您希望预订房间的日期是什么?键入日期dd/mm/yyyy:”)
尝试:
datestart=dt.datetime.strtime(datestart,dateformat)#检查格式是否匹配
除值错误外:
打印(“格式错误”)#告诉用户格式错误
#让他们再试一次
其他:
如果datestart.date()<今天:
打印(“无效日期”)
其他:
str_datestart=datestart.strftime(“%d/%m/%Y”)#这将去掉不必要的部分
break#如果未发现错误,则继续执行程序
尽管如此:
#允许用户输入预订结束的日期
dateend=输入(“您希望预订结束的日期?键入数据dd/mm/yyyy:”)
尝试:
dateend=dt.datetime.strtime(dateend,dateformat)
除值错误外:
打印(“格式不正确”)
其他:
如果datestart.date()>dateend:#抛出错误
打印(“输入有效日期”)
其他:
str_dateend=dateend.strftime(“%d/%m/%Y”)#这将去掉不必要的部分
打破
添加问题中出现的错误。。。这将帮助我们更容易地回答您这不是一个错误,对不起。它只是不起作用,它不比较我下面回答的日期