using recursion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wagn31
    New Member
    • Jun 2007
    • 6

    using recursion

    new to python and I have this assingment: I need to write a recursive function that takes paramaters (aString,nTimes ) and it needs to print the aString parameter the number of times specified by nTimes. I cannot use a for or while loop, just recursion. How would I go about doing this? all i know how to do is get it started and use recursion to call function again.

    def printNtimes(aSt ring,nTimes):

    I also know how to do it using recursion with one parameter:
    [CODE=python]
    def n_lines(n):
    if n>0:
    print "Line"
    n_lines(n-1)[/CODE]
    Last edited by wagn31; Jul 24 '07, 04:14 AM. Reason: added
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by wagn31
    new to python and I have this assingment: I need to write a recursive function that takes paramaters (aString,nTimes ) and it needs to print the aString parameter the number of times specified by nTimes. I cannot use a for or while loop, just recursion. How would I go about doing this? all i know how to do is get it started and use recursion to call function again.

    def printNtimes(aSt ring,nTimes):

    I also know how to do it using recursion with one parameter:
    [CODE=python]
    def n_lines(n):
    if n>0:
    print "Line"
    n_lines(n-1)[/CODE]
    It's just the same; no big change:[CODE=python]def n_lines(anStr, n):
    if n>0:
    print anStr
    n_lines(anStr, n-1)


    n_lines("hello" , 5)[/CODE]

    To learn how to do the pretty printing (which I've added to your post), please read the "REPLY GUIDELINE" on the right hand side of the page while you reply. They are required under the rules of our Posting Guidelines. Thanks

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by bartonc
      It's just the same; no big change:[CODE=python]def n_lines(anStr, n):
      if n>0:
      print anStr
      n_lines(anStr, n-1)


      n_lines("hello" , 5)[/CODE]

      To learn how to do the pretty printing (which I've added to your post), please read the "REPLY GUIDELINE" on the right hand side of the page while you reply. They are required under the rules of our Posting Guidelines. Thanks
      A very minor change:[CODE=python]def n_lines(anStr, n):
      if n:
      print anStr
      n_lines(anStr, n-1)[/code]

      Comment

      Working...