Rodney
Rodney

Reputation: 29

Different outputs depending on different IDE used?

So I'm learning Python in a Coursera Google course, which provides instructional code inside a Jupyter notebook page. But in addition to the Jupyter page, I primarily use the IDLE Shell and scratchpad (downloaded from Python.org) as my main IDE to run programs. The output is generally identical when copy/pasting code between these two environments.

But this one code chunk produces different output between the two environments. In the Jupyter it provides all the pairings, line-by-line, all the way down. But the IDLE Shell does not print out anything; its totally blank, despite being a precise copy/paste. I can't make sense of these different results. I have really tried to make sure there was no typo anywhere. Any ideas? Thanks

dominoes = []
for left in range(7):
    for right in range(left, 7):
        dominoes.append((left, right))
dominoes

Upvotes: -1

Views: 119

Answers (4)

Terry Jan Reedy
Terry Jan Reedy

Reputation: 19184

If one pastes the statements one at a time into IDLE Shell, or the standard PyREPL, they are executed one at a time and you get the result expected.

>>> dominoes = []
>>> for left in range(7):
...     for right in range(left, 7):
...         dominoes.append((left, right))
... 
>>> dominoes
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 3), (3, 4), (3, 5), (3, 6), (4, 4), (4, 5), (4, 6), (5, 5), (5, 6), (6, 6)]

If one pastes all three at once into Shell, one gets SyntaxError: multiple statements found while compiling a single statement. Shell does not currently try to separate multiple pasted statements.

If one pastes all three at once into the current 3.14.0a1+ PyREPL, the last line is indented the same as the previous statement, resulting in two statements and the list being printed in each iteration of the double loop.

If one pastes all three at once into an editor, IDLE's or other, and, as other said, wrap dominoes with print(), the code executes (in batch mode) as expected, with the output above.

Upvotes: 0

toyota Supra
toyota Supra

Reputation: 4560

It will would with any IDE.

Easier way to use one-liner.

Snippet:

dominoes = [(left, right)for left in range(7)for right in range(left, 7)] 
print(dominoes)

Upvotes: 0

Kaveh Movahedi
Kaveh Movahedi

Reputation: 166

try printing the dominoes variable

print(dominoes)

Jupyter automatically prints the output when you write a variable name in one line, but it is not the case in other shell or IDEs. you need to explicitly tell them to print the output on screen

Upvotes: 0

AKX
AKX

Reputation: 169338

This would work fine in a REPL, where the value of the last expression usually gets implicitly printed out.

However, to print output in general, you'd want print(dominoes) instead of dominoes.

Upvotes: 0

Related Questions