-1

The exercise is to create a recursive function that accepts another function and an argument. I've encountered an semantic error, and my function is not doing what I had hoped it would do. I'm a beginner, so this may be a very easy fix, but I can't seem to find it. Thank you for any help you can provide:

def do_this(func, arg):   # this function will call another function (func) a maximum of (arg) times
                          
    arg -= 1              # in order to ensure that we call (func) only (arg) times, I reduce (arg) by 1 
                           #to start
    if arg <= 0:
        return
    else:
        do_this(func, arg) # here is where the recursion takes place when this function calls itself - if 
                           # this is written correctly, then this function will call itself (arg) times


def print_n(s, n):        # this function is also recursive, it will print the arguments passed to it 
                          #from the function 'do_this'
      if n <= 0:          # each time it completes a cycle of 'n' iterations it will print the bundle 
                          #number
        x = 1
        print("Bundle Number", x)
        x += 1
        return
      else:
        print(s)
        print_n(s, n - 1)

do_this(print_n("hello", 2), 4)

This should print the following:

hello
hello
Bundle Number 1
hello
hello
Bundle Number 2
hello
hello
Bundle Number 3
hello
hello
Bundle Number 4
3
  • At no stage does do_this call its func param. Commented Sep 16, 2020 at 20:04
  • 1
    How to debug small programs. The point about using a debugger to step through your code is especially helpful: it lets you see exactly what each line of your program does. Commented Sep 16, 2020 at 20:05
  • 1
    Furthermore, you aren't passing a function to do_this in your driver code: do_this(print_n("hello", 2), 4), you are passing the result of calling print_n("hello", 2), 4) which is None, not a function Commented Sep 16, 2020 at 20:06

2 Answers 2

2

do_this calls itself but never actually calls func.

Also, you shouldn't pass parameters to print_n before passing it to do_this - you should pass the function itself along with all the data you need to perform the calls. Passing parameters will cause the call to print_n to be actually executed. Normally, the return value of print_n would then be passed to do_this, except that there is no return value.

Remember, you want to pass print_n itself to do_this, not just the result of print_n.

Sign up to request clarification or add additional context in comments.

Comments

-3

I noticed a small issue in your code. It appears that the func isn't being called as expected, likely due to how you're passing print_n("hello", 2) to do_this.

For a solution, consider using a lambda function or directly referencing print_n. Additionally, if you're interested in a deeper understanding, exploring Asymptotic Analysis could be valuable. You can find a helpful introduction about it here https://youtu.be/BpiMRyWoDu0?si=tvyj-56gUmjt0p0r .

Understanding this concept may provide insights into why passing functions in this way can behave unexpectedly. Feel free to reach out if you have any questions or need further clarification. Happy coding!

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.