我试图了解如何根据当前时间和Python中的时间范围来创建字符串。因此,如果是在上午8点到上午11点之间,则Python会确定现在是几点,并自动返回“早餐”字符串;如果它在上午11点到下午4点之间->返回午餐字符串
答案 0 :(得分:0)
首先,请在询问时保持清晰,并提供一些您之前尝试过的示例。 花费时间很容易,因为:
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
您可以查看有关日期时间模块here
的更多信息因此,current_time带一个字符串与时间。要选择分钟或小时,您需要选择他们所在的位置。
例如:
myTime = "12:34:56"
seconds = myTime[-2:]
print(seconds)
56
有关切片字符串的更多示例,here
您想要整数而不是字符串?
seconds = int(seconds)