Python Forum
Why doesn't this work in the Python Shell?
Thread Rating:
  • 3 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why doesn't this work in the Python Shell?
#1
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
Reply
#2
(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)
Reply
#3
What was the error message?
Reply
#4
Works if you use the ">Code" button at the top of the forum page as well  Smile
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
(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).
Reply
#6
(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.
Reply
#7
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)
Reply
#8
(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.
Reply
#9
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')
Reply
#10
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help creating shell scrip for python file marciokoko 10 1,254 Sep-16-2023, 09:46 PM
Last Post: snippsat
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 870 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Launch Python IDLE Shell from terminal Pavel_47 5 1,143 Feb-17-2023, 02:53 PM
Last Post: Pavel_47
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,679 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 838 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 1,655 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 4,050 May-30-2022, 03:31 PM
Last Post: bowlofred
  batch file for running python scipt in Windows shell MaartenRo 2 1,824 Jan-21-2022, 02:36 PM
Last Post: MaartenRo
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 1,929 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,657 Oct-30-2021, 11:20 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020