2016-08-03 3 views
1

так вот моя проблема, я пытаюсь сделать небольшую программу, которая дает пользователю следующую дату, когда ему придется платить арендную плату.Дата обновления в 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.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) 

У меня есть проблема в том, что rent_date всегда остается неизменным (2015-02-01) Любая идея, почему?

+2

['date.replace'] (https://docs.python.org/2/library/datetime.html#datetime.date.replace) возвращает новую дату. Вы вызываете 'rd.replace', но не получаете результат. Вероятно, вам нужно вернуть новую дату из «rent_date_calc» и распечатать ее. – khelwood

ответ

1

Ваш код ничего не меняет, потому что datetime является неизменным объектом, а когда вы вызываете замену на нем, он возвращает новое datetime вместо изменения первого.

Вы должны return новый объект из функции и assign его обратно rent_date:

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) 
+1

Ты зря. Спасибо @pistache за объяснение. Вы должны [принять ответ] (http://stackoverflow.com/help/accepted-answer). –

0

Ваши функции должны возвращать новую дату аренды. Вам просто нужно добавить следующие строки в коде:

  • возвращение кд
  • new_rent_date = rent_date_calc (CURDATE, rent_date)

==== ================================================== ==============

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) 
+0

'cd' и' rd' не являются изменениями в функции –

+0

Действительно спасибо @OhadEytan. – datahero