Nov-07-2022, 09:42 PM
There is one print, the print in the function named Q9().
If that doesn't resolve your confusion, there is one other thing that may be producing extra output in a jupyter notebook.
Jupyter is a bit odd in that it automatically prints results without having to call print(). If you call a function and it returns a result that is not None, jupyter automatically prints the result. I modified Q(n) to return n. Jupyter will see a non-None result and print it. This code will print the numbers, then it prints n.
def Q9(n): for i in range(n): print(i) Q9(5)
Output:0
1
2
3
4
As rob101 said, you can modify the print command to put all the output on 1 line.def Q9(n): # n is an arbitrary variable name for i in range(n): print(i, end=" ") # This prints numbers Q9(5) # This calls the function that prints the numbers
Output:0 1 2 3 4
jIn both instances there is 1 print for each number counting up from 0 to n. It is just a matter of printing all the numbers on one line or separate lines.If that doesn't resolve your confusion, there is one other thing that may be producing extra output in a jupyter notebook.
Jupyter is a bit odd in that it automatically prints results without having to call print(). If you call a function and it returns a result that is not None, jupyter automatically prints the result. I modified Q(n) to return n. Jupyter will see a non-None result and print it. This code will print the numbers, then it prints n.
def Q9(n): for i in range(n): print(i, end=" ") return(n) Q9(5)
Output:0 1 2 3 4
5
The automatic printing can be suppressed by adding a semicolon to the end of the line.def Q9(n): for i in range(n): print(i, end=" ") return(n) Q9(5);If you are seeing extra prints that you don't expect, look for function calls that return values. To stop them from printing, add semicolons to those lines.