2 回答

TA貢獻1796條經驗 獲得超4個贊
您正在與input_time.hour
整數進行比較。沒有這樣的整數 N 23 <= N <= 6
,因此您elif
語句中的條件永遠不會為真。您應該簡單地將elif
語句替換為else
.

TA貢獻1111條經驗 獲得超0個贊
如果您輸入的時間為:10:24,則此:datetime.datetime.strptime(startime,"%H:%M") 將起作用。如果您將時間輸入為 10,您的示例將起作用。
一切都取決于您如何定義時間輸入。
另外,你的比較似乎不對。
import datetime
distance_tobecovered = float(input("Please enter a number for the distance: "))
startime = input("Please input the time for the alarm in format HH:MM :")
fixed_charge = 3.5
perkilo_charge = 2.1 * distance_tobecovered
valueforall = fixed_charge + perkilo_charge
v = valueforall + (2 * distance_tobecovered * 0.99)
input_time = datetime.datetime.strptime(startime, "%H:%M")
if ((input_time.hour>=23) or (input_time.hour < 6)):
print("night or early morning")
print(v)#does some calculation
else:
print("day")
print("#does some calculation ")
添加回答
舉報