Python Forum
Why doesn't this work in the Python Shell? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why doesn't this work in the Python Shell? (/thread-616.html)

Pages: 1 2


Why doesn't this work in the Python Shell? - diemildefreude - Oct-24-2016

Hey, this simple code works as a script, but not if I enter it directly into the Python Shell (via Cmder on Windows). Is that normal?

i = 1
while i <= 6:
    print 2 * i, '   ',
    i += 1
print



RE: Why doesn't this work in the Python Shell? - buran - Oct-24-2016

(Oct-24-2016, 06:52 AM)diemildefreude Wrote: Hey, this simple code works as a script, but not if I enter it directly into the Python Shell (via Cmder on Windows). Is that normal?
i = 1
while i <= 6:
    print 2 * i, '   ',
    i += 1
print
I think you mistake the Python Shell and the Command prompt on Windows. It works just fine in Python Shell (Python2)


RE: Why doesn't this work in the Python Shell? - Larz60+ - Oct-24-2016

What was the error message?


RE: Why doesn't this work in the Python Shell? - sparkz_alot - Oct-24-2016

Works if you use the ">Code" button at the top of the forum page as well  Smile


RE: Why doesn't this work in the Python Shell? - snippsat - Oct-24-2016

(Oct-24-2016, 06:52 AM)diemildefreude Wrote: but not if I enter it directly into the Python Shell (via Cmder on Windows). Is that normal?
From cmder,just the same cmd.
C:\
λ cd python27

C:\Python27
λ python
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> i = 1
>>> while i <= 6:
...     print 2 * i, '   ',
...     i += 1
...
2     4     6     8     10     12
>>> exit()

C:\Python27
λ
Only 1 line at time an enter.
... here you push 4 space before enter.

I never use shell(interactive interpreter) from cmd/cmder.
That's much better from and IDE editor as eg PyCharm(Community Free),Spyder,Pyscripter ect.
The auto indent and autocomplete work in shell(interactive interpreter).


RE: Why doesn't this work in the Python Shell? - diemildefreude - Oct-26-2016

(Oct-24-2016, 01:53 PM)snippsat Wrote:
(Oct-24-2016, 06:52 AM)diemildefreude Wrote: but not if I enter it directly into the Python Shell (via Cmder on Windows). Is that normal?
From cmder,just the same cmd.
C:\
λ cd python27

C:\Python27
λ python
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> i = 1
>>> while i <= 6:
...     print 2 * i, '   ',
...     i += 1
...
2     4     6     8     10     12
>>> exit()

C:\Python27
λ
Only 1 line at time an enter.
... here you push 4 space before enter.

I never use shell(interactive interpreter) from cmd/cmder.
That's much better from and IDE editor as eg PyCharm(Community Free),Spyder,Pyscripter ect.
The auto indent and autocomplete work in shell(interactive interpreter).

Ah, this works, but there's a difference from my posted code. There's no second print at the end. If I type the second "print", I get an invalid syntax error about the "print". Same thing with both Cmder and the actual Python Shell. Not a big deal, I guess, just wondering why the extra print works as a script, but not in the shell.


RE: Why doesn't this work in the Python Shell? - Larz60+ - Oct-26-2016

the 2nd print statement will simply print a end of line.
since the loop is not doing this, the effect is to finish the line of the loop,
not and extra line.

if you change it to print some text you will see this (at the end of the loop print on same line)


RE: Why doesn't this work in the Python Shell? - diemildefreude - Oct-26-2016

(Oct-26-2016, 03:44 AM)Larz60+ Wrote: the 2nd print statement will simply print a end of line.
since the loop is not doing this, the effect is to finish the line of the loop,
not and extra line.

if you change it to print some text you will see this (at the end of the loop print on same line)

Ah, I think I see the problem. the last print is not related to the loop, so it's processed as an error if you enter it in the ... at the end of the loop statement. But in a script, it's perceived as a new statement.


RE: Why doesn't this work in the Python Shell? - Larz60+ - Oct-26-2016

It shouldn't be an error in a script either.
think of it this way, in order to keep your main printout on a single line,
the print doesn't include a line feed.
the print statement without arguments will provide that line feed and nothing else.
If you want a blank line after the printout, you will need to supply two.
print('\n\n')


RE: Why doesn't this work in the Python Shell? - snippsat - Oct-26-2016

(Oct-26-2016, 06:23 AM)diemildefreude Wrote: Ah, I think I see the problem. the last print is not related to the loop, so it's processed as an error if you enter it in the ... at the end of the loop statement. But in a script, it's perceived as a new statement.
Yes this is is correct shell(interactive interpreter) execute line by line or a block in one go.
Therefor will the print statement at end of while block give syntax error. 
In a script it will work fine.
i = 1
while i <= 6:
   print 2 * i, '   ',
   i += 1
print 'aaaaaaaaa'
Output:
2     4     6     8     10     12     aaaaaaaaa
As a new user in Python you should be thinking of starting with Python 3 it's the future.
Same script and end='' is clearer than just a comma for prevents a new line to be emitted.
i = 1
while i <= 6:
    print(2 * i, end= '   ')
    i += 1
print('aaaaaaaaa')
Output:
2   4   6   8   10   12   aaaaaaaaa