Posts: 18
Threads: 5
Joined: Oct 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
Posts: 8,168
Threads: 160
Joined: Sep 2016
Oct-24-2016, 07:02 AM
(This post was last modified: Oct-24-2016, 07:05 AM by buran.)
(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)
Posts: 12,041
Threads: 487
Joined: Sep 2016
What was the error message?
Posts: 1,298
Threads: 38
Joined: Sep 2016
Works if you use the ">Code" button at the top of the forum page as well
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
Posts: 7,324
Threads: 123
Joined: Sep 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).
Posts: 18
Threads: 5
Joined: Oct 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.
Posts: 12,041
Threads: 487
Joined: Sep 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)
Posts: 18
Threads: 5
Joined: Oct 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.
Posts: 12,041
Threads: 487
Joined: Sep 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')
Posts: 7,324
Threads: 123
Joined: Sep 2016
Oct-26-2016, 12:58 PM
(This post was last modified: Oct-26-2016, 01:06 PM by snippsat.)
(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
|