Note that there are some explanatory texts on larger screens.

plurals
  1. POPython comma separated print changes function behavior
    primarykey
    data
    text
    <p>Although I've seen other questions on Python delimiters I haven't been able to find a question answering this so here goes. I'm writing a function that will recursively print an array backwards, and it appears to work just fine:</p> <pre><code>def print_array_backwards(array): if (len(array) == 1): print array[0], return print_array_backwards(array[1:]) print array[0], ########################################## x = [0, 1, 2, 3, 4, 0, 1, 2, 3, 4] print 'Array = ', x print_array_backwards(x) </code></pre> <p>Will output:</p> <pre><code>Array = [0, 1, 2, 3, 4, 0, 1, 2, 3, 4] 4 3 2 1 0 4 3 2 1 0 </code></pre> <p>The problem is when I try to print the output of the print_array_backwards function in line with other text, like this:</p> <pre><code>print 'Array = ', x print 'The array backwards is', print_array_backwards(x) </code></pre> <p>This will output:</p> <pre><code>Array = [0, 1, 2, 3, 4, 0, 1, 2, 3, 4] The array backwards is 4 3 2 1 0 4 3 2 1 0 None </code></pre> <p>My question is, where does this extra None value come from??</p> <p>If I push the function output to the next line but leave the comma with the print statement like this:</p> <pre><code>print 'Array = ', x print 'The array backwards is', print_array_backwards(x) </code></pre> <p>I obviously get the desired output:</p> <pre><code>Array = [0, 1, 2, 3, 4, 0, 1, 2, 3, 4] The array backwards is 4 3 2 1 0 4 3 2 1 0 </code></pre> <p>Can anyone help me understand why leaving the function in the same line of the print statement causes the function to behave differently? </p> <p>Thanks for your time!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload