Python Forum
Print name N times using recursion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print name N times using recursion
#1
How can i write program to Print "Hello" 5 times using recursion.

I am trying code below but it is not working.
def fnc():
    for i in range(5):
     print("Hello")
    fnc()
    

fnc()    
Reply
#2
Not sure why you are calling func() inside the function, but this worked for me:

def func():
    for i in range(5):
        print ("Hello")

func()
Maybe your IDE is detecting an infinite loop? Your current code will cause a stack overflow.
Reply
#3
(Oct-23-2019, 05:31 AM)SouthernYankee Wrote: Not sure why you are calling func() inside the function
Note that OP assignment is asking them to use recursion... They try to do that although unsuccessfully as they end up with infinite recursion
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(Oct-23-2019, 05:34 AM)buran Wrote: Note that OP assignment is asking them to use recursion... They try to do that although unsuccessfully as they end up with infinite recursion

Ah. Didn't catch that this was a homework assignment. Makes since now. Still getting used to this forum. I'll be more careful in the future.
Reply
#5
You need to get rid of the loop, just print one time per function call. Then you need a terminating condition that stops the recursion. You would typically pass the value that terminates as a parameter to the function, and modify it each time you make the recursive call.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(Oct-23-2019, 01:35 PM)ichabod801 Wrote: You need to get rid of the loop, just print one time per function call. Then you need a terminating condition that stops the recursion. You would typically pass the value that terminates as a parameter to the function, and modify it each time you make the recursive call.

Please share code for this program ...
Reply
#7
Eliminate the for loop, add a parameter to fnc() that will be the counter. In the function increment the counter, print the name, do a test to see if the parameter is now 5, and call fnc(x) again if it is not.

Will guide but not write your homework assignment
Reply
#8
(Oct-23-2019, 02:24 PM)ift38375 Wrote: Please share code for this program ...

Please try to code this program yourself, and if it doesn't work I will help you fix it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Recursion and permutations: print all permutations filling a list of length N SantiagoPB 6 3,264 Apr-09-2021, 12:36 PM
Last Post: GOTO10
  GCF function w recursion and helper function(how do i fix this Recursion Error) hhydration 3 2,524 Oct-05-2020, 07:47 PM
Last Post: deanhystad
  Find how many times a user played an artist and how many times disruptfwd8 1 2,579 May-04-2018, 08:32 AM
Last Post: killerrex

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020