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
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 586 Jul-17-2021, 04:19 PM
Last Post: bill_z
  string function doesn't work in script ClockPillow 3 305 Jul-13-2021, 02:47 PM
Last Post: deanhystad
Photo Windows 10 PowerShell doesn't work Amy 3 1,100 Apr-27-2021, 01:33 PM
Last Post: jefsummers
  Why doesn't this print statement work? stylingpat 10 1,035 Mar-23-2021, 07:54 PM
Last Post: buran
  keyboard module doesn't work in the microsoft version terminal of python. username 1 546 Feb-25-2021, 05:19 PM
Last Post: Larz60+
  pygame music doesn't work CompleteNewb 1 1,595 Jan-30-2021, 09:25 AM
Last Post: Tyrel
  Curses script doesn't work wavic 1 694 Jan-08-2021, 09:11 PM
Last Post: wavic
  why doesn't python look in two directions Kakha 21 1,507 Jan-01-2021, 11:24 PM
Last Post: jefsummers
  My code doesn't work, can someone help me? aldasrasickas 5 728 Dec-21-2020, 02:26 PM
Last Post: aldasrasickas
  If, elif, else doesn't work well Vidar567 5 914 Nov-21-2020, 06:25 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

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