Search code examples
pythonpython-3.xinterpreterpython-idle

Different results between Python shell and IDLE


The following code is printing the sum of multiples of 5 from 1 to 1000

s = 0
for i in range(1, 1001):
    if i % 5 == 0: s += i
print(s)

If I run this code in IDLE, the result is 100500, but in the shell there's an error SyntaxError: invalid syntax, coming out at print. Why do IDLE and shell give different results? My Python version is 3.7.


Solution

  • In Python shell (canonical name: REPL) you're expected to terminate an indented block with an empty line, so you should run this in REPL:

    s = 0
    for i in range(1, 1001):
        if i % 5 == 0: s += i
    
    print(s)
    

    Note the empty line before print, that's required in REPL but not when you run the code from a file (or IDLE).