Python Forum
Odd interpreter behavior - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Odd interpreter behavior (/thread-23612.html)



Odd interpreter behavior - sumncguy - Jan-08-2020

Im posting this in Homework as I am taking a self paced course.

Im on a Centos 6 vm.
There is both python 2.7.5 and 3.7 loaded.
I am not sure why I am getting the error given below.
Post preview shows that the alignment is incorrect.
The output = f.read() is lined up with the o in open
The print statement is lined up with the w in the with.


@localhost ~]$ python3.7
Python 3.7.4 (default, Jan  8 2020, 08:43:10) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> with open("shver.txt") as f:
...      output = f.read()
... print(output)
  File "<stdin>", line 3
    print(output)
        ^
SyntaxError: invalid syntax
>>> 
Any help would be appreciated.

Thanks
Sum


RE: Odd interpreter behavior - buran - Jan-08-2020

output = f.read() should be indented one level.
the print function may be inside the context manager or out:
>>> with open("shver.txt") as f:
...     output = f.read()
...     print(output)
>>> with open("shver.txt") as f:
...     output = f.read()
>>> print(output)



RE: Odd interpreter behavior - sumncguy - Jan-08-2020

Thanks ..

it is actually .. for some reason the forum interface doesnt like the paste of the resulting error.


RE: Odd interpreter behavior - buran - Jan-08-2020

Actually it would be

>>> with open("shver.txt") as f:
...     output = f.read()
...
>>> print(output)
i.e. you need to exit the context manager by entering blank line


RE: Odd interpreter behavior - sumncguy - Jan-09-2020

yes. Thank you.

I played around last night and found that, yes, the print should be outside of the existing block.