我正在用python(3.10.1)做一个程序,其中我需要比较输入,在本例中是一个月的数字(例如may为05)与本地当前时间。
from datetime import datetime
month=int(input("insert the number of a month: "))
dt_obj = datetime.now()
dt_frt = dt_obj.strftime("%m")
print(dt_frt)
if (month==dt_frt):
print("the month you selected is equal to the current month")
然后停止打印当前月份,但不执行最后两行所述的操作,也不给出任何错误。有人能帮帮我吗?谢谢。
我认为你是在比较int
和str
,这就是为什么if条件不为真。要解决这个问题,您可以删除int(input(...))
或在dt_frt
周围添加str(...)
您也可以使用date.month
(doc)获取月份