为什么这个功能不起作用:Python

时间:2016-12-11 04:22:14

标签: python

我想制作一个循环,今天的日期会下降,直到它与较早的日期相匹配。这是我的代码:

from datetime import date
Months = ["January","February","March","April","May","June","July","August","September","October","November","December"]
today = date.today()

A= ("%s %s" % (Months[today.month - 1],today.day))
end_date= "December 1"


while A != end_date:
    a=today.day
    a=a-1
    A= ("%s %s" % (Months[today.month - 1],a))
    print(A)

为什么我的循环在12月9日被卡住,如果我在循环“a”中指定减少直到“A”等于“end_date”?

1 个答案:

答案 0 :(得分:0)

a=today.day

需要在循环之外:

from datetime import date
Months = ["January","February","March","April","May","June","July","August","September","October","November","December"]
today = date.today()

A= ("%s %s" % (Months[today.month - 1],today.day))
end_date= "December 1"

a=today.day
while A != end_date:
    #a=today.day
    a=a-1
    A= ("%s %s" % (Months[today.month - 1],a))
    print(A)

你将a设置为当前日期 - 1,每次循环运行,所以它永远不会减少。