我正在嘗試比較兩個 datetime.time 值,但出現此錯誤:
檔案“c:\Users\xxxxxx\Desktop\pyfiche\code.py”,第 33 行,如果 (a.time >= d0.time() and a.time() <= d6.time()) 或(a.time() >= d21.time() and a.time() <= d23.time()): TypeError: '>=' 在 'builtin_function_or_method' 和 'datetime.time' 的實體之間不支持
這是我的代碼:
def timeofweek(a):
d6 = a.replace(hour=6, minute=0, second=0, microsecond=0)
d21 = a.replace(hour=21, minute=0, second=0, microsecond=0)
d0 = a.replace(hour=0, minute=0, second=0, microsecond=0)
d23 = a.replace(hour=23, minute=0, second=0, microsecond=0)
if a.weekday() in range(0,6):
print(a.time(),type(a.time())) #for debugging and they are both Datetime.time
print(d0.time(),type(d0.time())) #for debugging and they are both Datetime.time
if (a.time() >= d6.time() and a.time() < d21.time()):
return 1
if (a.time() >= d0.time() and a.time() < d6.time()) or (a.time() >= d21.time() and a.time() <= d23.time()):
return 2
if a.weekday() == 6:
if (a.time() >= d6.time() and a.time() <= d21.time()):
return 2
if (a.time >= d0.time() and a.time() <= d6.time()) or (a.time() >= d21.time() and a.time() <= d23.time()):
return 3
我確定 d6、d21、d0、d23 都是相同型別的 Datetime.time
當輸入值從 24h 格式更改為 12h 格式時,問題就開始了,但是當我列印除錯值時,我仍然每次都得到 24h 格式。
uj5u.com熱心網友回復:
您沒有在倒數第二行中為 a 呼叫 .time() 方法。(你缺少括號)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/537301.html
