Python中的更新日期
所以这就是我的问题,我正在尝试做一个小程序,让用户知道下一个必须支付租金的日期 这是我的密码:Python中的更新日期,python,Python,所以这就是我的问题,我正在尝试做一个小程序,让用户知道下一个必须支付租金的日期 这是我的密码: curdate = datetime.date(2015, 01, 01) rent_date = datetime.date(curdate.year, curdate.month+1, 01) one_day = datetime.timedelta(days = 1) one_week = datetime.timedelta(weeks = 1) one_month = datetime.t
curdate = datetime.date(2015, 01, 01)
rent_date = datetime.date(curdate.year, curdate.month+1, 01)
one_day = datetime.timedelta(days = 1)
one_week = datetime.timedelta(weeks = 1)
one_month = datetime.timedelta(weeks = 4)
def rent_date_calc(cd, rd):
if cd.month == 12:
rd.replace(cd.year+1, 01, 01)
else:
rd.replace(cd.year, cd.month+1, 01)
def time_pass(rd, cd, a, al):
if rd < cd:
for a in al:
a.finances -= a.rent
move_fwd = raw_input("Would you like to move forward one day(1), one week (2) or one month (3)?")
if move_fwd == "1":
curdate += one_day
elif move_fwd == "2":
curdate += one_week
else:
curdate += one_month
time_pass(rent_date, curdate, prodcomp, prodcomps)
rent_date_calc(curdate, rent_date)
print "Rent date: " + str(rent_date)
curdate=datetime.date(2015,01,01)
租金=日期时间日期(curdate.year,curdate.month+1,01)
一天=datetime.timedelta(天=1)
一周=datetime.timedelta(周=1)
一个月=datetime.timedelta(周=4)
定义租金日期计算(cd、rd):
如果cd.month==12:
研发更换(光盘年份+1,01,01)
其他:
rd.更换(cd.年,cd.月+1,01)
def时间(rd、cd、a、al):
如果rd
我的问题是租期总是保持不变(2015-02-01)
你知道为什么吗?你的代码没有改变任何东西,因为datetime是一个不可变的对象,当你对它调用replace时,它会返回一个新的datetime,而不是修改第一个 您应该
从函数中返回
新对象,并将其分配回租金日期
:
def rent_date_calc(cd, rd):
if cd.month == 12:
return rd.replace(cd.year+1, 01, 01)
else:
return rd.replace(cd.year, cd.month+1, 01)
...
rent_date = rent_date_calc(curdate, rent_date)
您的函数必须返回新的租金日期。您只需在代码中添加以下行:
- 返回cd
- 新租金日期=租金日期计算(当前日期、租金日期)
====================================================================
curdate = datetime.date(2015, 1, 1)
rent_date = datetime.date(curdate.year, curdate.month+1, 1)
one_day = datetime.timedelta(days = 1)
one_week = datetime.timedelta(weeks = 1)
one_month = datetime.timedelta(weeks = 4)
def rent_date_calc(cd, rd):
if cd.month == 12:
new_date = rd.replace(cd.year+1, 1, 1)
else:
new_date = rd.replace(cd.year, cd.month+1, 1)
return new_date
def time_pass(rd, cd, a, al):
if rd < cd:
for a in al:
a.finances -= a.rent
# Not sure what this function should return...
move_fwd = raw_input("Would you like to move forward one day(1), one week (2) or one month (3)?")
if move_fwd == "1":
curdate += one_day
elif move_fwd == "2":
curdate += one_week
else:
curdate += one_month
# time_pass(rent_date, curdate, prodcomp, prodcomps)
new_rent_date = rent_date_calc(curdate, rent_date)
print "Rent date: " + str(new_rent_date)
curdate=datetime.date(2015,1,1)
租金\日期=datetime.date(curdate.year,curdate.month+1,1)
一天=datetime.timedelta(天=1)
一周=datetime.timedelta(周=1)
一个月=datetime.timedelta(周=4)
定义租金日期计算(cd、rd):
如果cd.month==12:
新日期=第三次更换(光盘年份+1,1,1)
其他:
新日期=第三次更换(cd.年,cd.月+1,1)
返回新日期
def时间(rd、cd、a、al):
如果rd
返回新日期。您正在调用rd.replace
,但未捕获结果。您可能需要从rent\u date\u calc
返回新的日期并打印出来。您很抱歉。感谢@pistache的解释。您应该这样做。cd
和rd
不是功能上的更改