以下是一个示例代码,用于根据当前时间将时间分类为白天或黑夜:
import datetime
def classify_time():
current_time = datetime.datetime.now().time()
if current_time >= datetime.time(6) and current_time < datetime.time(18):
return "白天"
else:
return "黑夜"
print(classify_time())
这段代码使用了datetime模块来获取当前时间。然后,它将当前时间与早上6点和晚上6点进行比较,如果当前时间在这两个时间之间,则被分类为"白天",否则被分类为"黑夜"。最后,代码打印出分类结果。
请注意,这个示例代码基于当前时间进行分类,因此它将根据你运行代码的时间不同而产生不同的结果。你可以根据自己的需要对时间范围进行调整。