Can Someone tell me what exactly is happening here.Is the print statement executing after all the draw [3,2,1] are completed or it's happening simultaneously.I tried adding print(n) but still couldn't figure out. Is it unpacking after storing the values of ('#'*n).I am getting what I desired but just needed to understand what is actually happening
def draw(n:int):
if n<0:
return
draw(n-1)
print ('#'*n)
draw(3)
the print comes after executing all the draw calls. if you want to print it in the same order make sure print comes first before calling the draw again
def draw(n:int):
if n<0:
return
print ('#'*n) # this should come first before the next call
draw(n-1)
to understand the sequence of the recursion, follow this visualization tool. just write your provided script and run it.
https://pythontutor.com/visualize.html#mode=edit
Related
def xyz(a):
i=0
while i>=a:
if i==777:
return"Jackpot"
else:
return"Try again"
i+=1
The above when i am running gives me no output. unable to figure out what's missing
There are many errors!
If you return before increment, you'll never have an increment.
If you return after increment , no point of while loop.
Solution
Simple if else
def xyz(a):
if a==777:
return "Jackpot"
else:
# i+=1
# return "Try again"
return ("Try Again")
Since you did not provide any information about what exactly you are trying to do all I can do is give you some advise based on what I can infer from your code. Notice that you are returning a value meaning you stop the execution of the while loop at the first iteration always (if you are 100% certain about the implementation of the function and your desired output is 'try again' then you have to 'catch' the returned value of the function and print that). Also check the while condition. I is initialised at 0 so (for me at least) it would make sense to check if i is less than a (but again this comes from my intuition about the code).
I'm making a Discord Bot and would like to know if it's possible to get what did an eval print.
I know you can start recording for prints when the eval starts and stop when the eval finishes and this way you'd get what was printed during the eval, but the program uses async and I'm afraid if I do that I could also get stuff printed outside of the eval.
Is there any way to get what was printed just during one eval?
Thank you!
I know using eval is bad, but I'm not asking whether if eval is good or bad.
I can't believe I didn't think of this before.
Set print to a function of yours which both logs the stuff and then passes it to the normal print function and make the eval use it, like this:
try: #Imagine this was in a try
printlog = []
print = yourfunction #yourfunction appends to printlog prints that
#go through it and then prints them normally
eval(stuff)
#printlog now has what was printed
And this way you'll get what was printed only inside that eval no matter anything else is running at the same time.
Is it possible to return from a function and continue executing code from just under the function. I know that may sound vague but here is an example:
def sayhi():
print("hi")
continue_function() #makes this function continue below in stead of return
#the code continues here with execution and will print hey
print("hey")
sayhi()
when executing this the code should do this:
it prints "hey"
it calls the sayhi() function
it prints "hi"
it calls a function to make it continue after the function (in theory similar behavour could be achieve by using decorators)
it prints "hey" again
it calls sayhi() again
etc
i am fully aware of the fact that similar behaviour can be achieved by just using for loops but for the project i am working on this functionality is required and not achievable by using looping.
some solutions i have thought of (but i have no clue how i could execute them) are:
somehow clearing the stack python uses to return from one function to another
changing return values
changing python itself (just to make clear: it would solve the problem but it is something i do not want to do beacuse the project must be usable on non-altered versions of python
using some c extension to change python's behaviour from within python itself
Repetition without loops can be done with recursion:
def sayhi():
print("hey")
print("hi")
sayhi()
sayhi()
I assume you have some terminating condition to insert. If not, this code will give a RecursionError.
This is probably a dumb question, but I'm new to programming and I have a recursive function set up that I'm trying to figure out. For any print function in Python, is it necessarily true that lines are printed in the order that they are written in the script OR for larger outputs, is it possible that smaller length outputs can get printed first in the console even though the print statement is later in the code (maybe due to some memory lag)?
Example:
def test_print():
#don't run this, but was meant for scale. Is there any chance the 1 would print before the list of lists?
print([[i for i in range(10000)] for j in range(10000)])
print(1)
Print statements pile output into stdout in the order the code was written. Top to bottom. It isn't possible any other way because that's the way the code is interpreted. Memory lag doesn't play any role here because the output to your console is a line for line rendition of the data that was piled into stdout. And the order the data was written to it can't change, so you'll maintain chronology. Of course, you can always play around with the how the print function itself works. But I wouldn't recommend tampering with standard library functions.
As said above, print() function is executed in the order which they are in your code. But you yourself can change the order in which you want it executed, after all you have every right to instruct the code to do whatever you want.
You'll always get the same order in the output as the order you execute print() functions in Python.
This code has me lost. When run, it outputs sequences which I finds strange:
def print_n(number):
if (number <= 0):
return None
else:
print number
print_n(number-1)
print_n(number-1)
print_n(4)
I thought it would output this sequence:
4,3,2,1,1,2,1,3,2,1
however it actually outputs:
4,3,2,1,1,2,1,1,3,2,1,1,2,1,1
I tried to draw the stack diagram of this function but when I get lost at the second appearance of the print_n(number-1).
I can understand this program without the second appearance of the print_n(number-1), as it's just normal recursion. However, the second print_n(number-1), seems much more complicated than I expected, I don't know how to trace this function call and explain the result...
Since the if block has an unconditional return, you can remove the else and the program will continue to behave the same way.
def print_n(number):
if (number <= 0):
return None
print number
print_n(number-1)
print_n(number-1)
Here, it's more apparent what is going on: You print number, and then call print_n twice using number-1. You can work backwards to derive the output.
print_n(1) prints "1"
print_n(2) prints "2" plus "1" plus "1": "211"
print_n(3) prints "3" plus "211" plus "211": "3211211"
print_n(4) prints "4" plus "3211211" plus "3211211": "432112113211211"
I liked the answer of Kevin, but let me add a few words towards "understanding recursion":
I often suggest using sheets of paper representing the stack. each sheet contains its local varaibles and current state - and you can mark the line you are "processing" with a pen.
Use a separate sheet as output / console.
This gives you a very good understanding of what is going on.
Of course, following your code in a debugger and examining the stack trace can be helpful as well. But try the paper-approach first!