Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plz trace this line by line
#1
#n natural number s
def func(q):
    if q>0:
       return func(q-1)
       return q
    elif q==0:
       return 0

print(func(5))
So I understood a lot of recursion now I want to print natural no's outside the function...help me trace the code y o/p is 0?
Yoriz write Jun-12-2021, 08:00 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
What do you think it is doing line by line?
which lines don't you understand what it is doing?
Reply
#3
(Jun-12-2021, 02:54 AM)Sure Wrote: So I understood a lot of recursion now I want to print natural no's outside the function...help me trace the code y o/p is 0?
You will have to explain what you want to accomplish. What I see is a function that returns "0" in a very inefficient way. And for negative input it returns "None". I can also see line 5 will never be reached.
What do you want the function to do? And why should it be recursive?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Βad Input on line 12 Azdaghost 5 1,322 Apr-19-2025, 10:22 PM
Last Post: Azdaghost
Question [SOLVED] [Beautiful Soup] Move line to top in HTML head? Winfried 0 309 Apr-13-2025, 05:50 AM
Last Post: Winfried
  Insert command line in script lif 4 1,038 Mar-24-2025, 10:30 PM
Last Post: lif
  Entry field random pull from list, each return own line Bear1981 6 849 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
  How to revert back to a previous line from user input Sharkenn64u 2 1,032 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  Pandas - error when running Pycharm, but works on cmd line zxcv101 2 2,475 Sep-09-2024, 08:03 AM
Last Post: pinkang
  Simplest way to run external command line app with parameters? Winfried 2 1,334 Aug-19-2024, 03:11 PM
Last Post: snippsat
  Printing the code line number arbiel 6 1,680 Jun-30-2024, 08:01 AM
Last Post: arbiel
  How to add multi-line comment section? Winfried 2 1,396 Jun-04-2024, 07:24 AM
Last Post: Gribouillis
Information Is it possible to multi line a Basic Function Construct line statement? If so how? BrandonKastning 7 2,038 May-23-2024, 03:02 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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