None returned?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • robinsiebler@gmail.com

    #1

    None returned?

    I can't figure out -what- is going wrong here. When the code reaches
    the 'return' line, there is data to be returned, but when it exits out
    to the calling function, 'None' is returned!

    import mx.DateTime

    def get_weeks(weeks , year, dates, date_list={}):
    if dates.has_key(y ear):
    date_list[year] = dates[year].keys()[-weeks:]
    if len(dates[year].keys()) >= weeks:
    return date_list
    else:
    weeks = weeks - len(dates[year].keys())
    get_weeks(weeks , str(int(year) -1), dates, date_list)

    def get_report_date s(weeks, dates):
    today = mx.DateTime.now ()
    this_week = today.iso_week[1]
    rpt_dates = get_weeks(weeks , str(today.year) , dates)
    print rpt_dates

    def main():

    dates = {'2006': {'50': [50, 'This is the 50th week'],
    '51': [51, 'This is the 51st week'],
    '52': [52, 'This is the 52nd week']},
    '2007': {'25': [1, 'This is the 1st week'],
    '26': [2, 'This is the 2nd week'],
    '27': [3, 'This is the 3rd week'],
    '28': [4, 'This is the 4th week'],
    '29': [5, 'This is the 5th week']}}

    get_report_date s(6, dates)

  • Kelvie Wong

    #2
    Re: None returned?

    On 7/5/07, robinsiebler@gm ail.com <robinsiebler@g mail.comwrote:
    I can't figure out -what- is going wrong here. When the code reaches
    the 'return' line, there is data to be returned, but when it exits out
    to the calling function, 'None' is returned!
    >
    import mx.DateTime
    >
    def get_weeks(weeks , year, dates, date_list={}):
    if dates.has_key(y ear):
    date_list[year] = dates[year].keys()[-weeks:]
    if len(dates[year].keys()) >= weeks:
    return date_list
    else:
    weeks = weeks - len(dates[year].keys())
    Right here.
    get_weeks(weeks , str(int(year) -1), dates, date_list)
    You have to change that line to:
    return get_weeks(weeks , str(int(year) -1), dates, date_list)

    Otherwise, if len(dates[year.keys()) < weeks, it doesn't return anything.
    >
    def get_report_date s(weeks, dates):
    today = mx.DateTime.now ()
    this_week = today.iso_week[1]
    rpt_dates = get_weeks(weeks , str(today.year) , dates)
    print rpt_dates
    >
    def main():
    >
    dates = {'2006': {'50': [50, 'This is the 50th week'],
    '51': [51, 'This is the 51st week'],
    '52': [52, 'This is the 52nd week']},
    '2007': {'25': [1, 'This is the 1st week'],
    '26': [2, 'This is the 2nd week'],
    '27': [3, 'This is the 3rd week'],
    '28': [4, 'This is the 4th week'],
    '29': [5, 'This is the 5th week']}}
    >
    get_report_date s(6, dates)
    >
    --

    >

    --
    Kelvie

    Comment

    • Steve Holden

      #3
      Re: None returned?

      robinsiebler@gm ail.com wrote:
      I can't figure out -what- is going wrong here. When the code reaches
      the 'return' line, there is data to be returned, but when it exits out
      to the calling function, 'None' is returned!
      >
      import mx.DateTime
      >
      def get_weeks(weeks , year, dates, date_list={}):
      if dates.has_key(y ear):
      date_list[year] = dates[year].keys()[-weeks:]
      if len(dates[year].keys()) >= weeks:
      return date_list
      else:
      weeks = weeks - len(dates[year].keys())
      get_weeks(weeks , str(int(year) -1), dates, date_list)
      >
      So if the else branch is taken here you end up dropping of the end of
      the function's code, which will return None.
      def get_report_date s(weeks, dates):
      today = mx.DateTime.now ()
      this_week = today.iso_week[1]
      rpt_dates = get_weeks(weeks , str(today.year) , dates)
      print rpt_dates
      >
      def main():
      >
      dates = {'2006': {'50': [50, 'This is the 50th week'],
      '51': [51, 'This is the 51st week'],
      '52': [52, 'This is the 52nd week']},
      '2007': {'25': [1, 'This is the 1st week'],
      '26': [2, 'This is the 2nd week'],
      '27': [3, 'This is the 3rd week'],
      '28': [4, 'This is the 4th week'],
      '29': [5, 'This is the 5th week']}}
      >
      get_report_date s(6, dates)
      >
      regards
      Steve
      --
      Steve Holden +1 571 484 6266 +1 800 494 3119
      Holden Web LLC/Ltd http://www.holdenweb.com
      Skype: holdenweb http://del.icio.us/steve.holden
      --------------- Asciimercial ------------------
      Get on the web: Blog, lens and tag the Internet
      Many services currently offer free registration
      ----------- Thank You for Reading -------------

      Comment

      Working...