所以,我有这本词典:
days_per_month = {"01": 31, "02": 28,
"03": 31, "04": 30,
"05": 31, "06": 30,
"07": 31, "08": 31,
"09": 30, "10": 31,
"11": 30, "12": 31}
这个功能:
def add_months(month):
"""
If needed, increment one day and check other parameters, such as months and years.
Requires:
- dmy str with a date represented as DD:MM:YY.
Ensures: str with the updated date represented as DD:MM:YY.
"""
if month == 2:
day = get_days(dmy)
if check_year(year) == "True":
if day > 29:
month += 1
day = 1
else:
if day > 28:
month += 1
day = 1
if days_per_month[month] = 31:
day = get_days(dmy)
if day > 31:
month += 1
day = 1
if days_per_month[month] = 30:
day = get_days(dmy)
if day > 30:
month += 1
day = 1
return month
函数get_days:
def get_days(dmy):
"""Get the number of days from a DD:MM:YY date representation.
Requires: dmy str with a date represented as DD:MM:YY.
Ensures: int with the number of days
"""
return int(dmy.split(':')[0])
功能check_year:
def check_year(year):
"""
Checks if the current year is a leap year or not.
Requires: year str with the year.
Ensures: 'True' if year is leap year; 'False' if year isn't a leap year.
"""
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
这是我正在尝试做的事情:我有一个先前的功能,我将x分钟增加到一定时间,让我们称之为y。想象一下,y =“23:56”,x =“5”,23:56 + 5 = 24:01。所以,我有另一个功能,在发生这种情况的日期增加一天。 现在我正在尝试完成改变月份的功能。例如: y = 23:56,x = 5,== 24:01。然而,之前的日期是31/12,现在是32/12:我用我之前的函数增加了一天,但现在我还要用函数add_months改变月份。所以,我查看了我的days_per_month字典并尝试查找月份(字典中的键)的值,这样我就可以获得该月的最大天数。我认为这是我应该这样做的,但我不断收到这个错误:
if days_per_month[month] = 31:
SyntaxError:语法无效
if days_per_month[month] = 30:
SyntaxError:语法无效
我做错了什么?
Obs1 - python 3.2 Obs2 - 如果您有任何改进我职能的建议,请告诉我!
答案 0 :(得分:0)
您的基本问题是您尝试分配值而不是比较它。使用
#!/bin/bash
awk '$2 > prev {print; prev = $2}'
代替if days_per_month[month] == 31:
但是我会建议像这样使用datetime:
if days_per_month[month] = 31:
我所做的是使用日期from datetime import *
a=datetime(2017,2,28,23,56,00)
b=a+timedelta(minutes=5)
初始化a
,然后将5分钟添加到此日期时间,并在28.02.2017 23:56:00
01.03.2017 00:01:00