EOF while scanning triple-quoted string literal

This is the place for queries that don't fit in any of the other categories.

EOF while scanning triple-quoted string literal

Postby linuxdesire » Mon Nov 18, 2013 9:36 pm

Ok, still following the python beginner book that I have. My first error one of the users said I was using python 2.7 and that user was correct. I now have python 3 and ran my code in the terminal but came up with EOF error. In the terminal I ran; python3 GameOver.py

Error code is: EOF while scanning triple-quoted string literal

Code: Select all
# Gamer OVer
# Demonstrates the use of quotes in strings

#!/usr/bin/env python3

print("Program 'Game Over' 2.0")

print("Same", "message", "as before")

print("Just"
        "a bit"
        " bigger")

print("Here", end=" ")
print("it is...")

print(
          """
)

Input("\n\nPress the enter key to exit.")
linuxdesire
 
Posts: 9
Joined: Fri Nov 15, 2013 6:45 pm

Re: EOF while scanning triple-quoted string literal

Postby micseydel » Mon Nov 18, 2013 10:00 pm

linuxdesire wrote:
Code: Select all
print(
          """
)

What is your intention here? Are you trying to print a single double-quote character (")? I'm afraid you have a misconception. This might help.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1390
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: EOF while scanning triple-quoted string literal

Postby linuxdesire » Mon Nov 18, 2013 10:02 pm

No, the book shows it printing GAME OVER in real big letters.
linuxdesire
 
Posts: 9
Joined: Fri Nov 15, 2013 6:45 pm

Re: EOF while scanning triple-quoted string literal

Postby Crimson King » Mon Nov 18, 2013 10:11 pm

Hi linuxdesire, the problem is on this line

Code: Select all
print(
          """
)


You got that error because your program reached the end of the file and didn't find the triple quotes that close the ones you opened on your print call.

If you wanted to print this character ("), do it like this:

Code: Select all
print( ' " ' )


Triple quotes are docstrings, take this funcion for example:

Code: Select all
def myfunction():
     """myfunction usage: blablabla"""
     return 1
myfunction.__doc__
>>>'myfunction usage: blablabla'


Further reading on strings here
User avatar
Crimson King
 
Posts: 122
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: EOF while scanning triple-quoted string literal

Postby micseydel » Mon Nov 18, 2013 11:26 pm

Crimson King wrote:Triple quotes are docstrings

Strictly speaking, PEPs 8 and 257 specify that doc strings should always use triple quotes, but they are not inherently docstrings (they can be used elsewhere, and as ad-hoc multi-line comments) and any string literal can be used as a docstring. Just so we're clear. And although it is prescribed that triple quotes should be used for docstring, it doesn't prohibit their use elsewhere.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1390
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: EOF while scanning triple-quoted string literal

Postby metulburr » Tue Nov 19, 2013 12:53 am

Triple quotes are docstrings

not all the time

Code: Select all
html = """Content-type: text/html\n

<html>
    <head>
        <title>{TITLE}</title>
        {CSS}
        <script></script>
    </head>
<body>
{BODY}
</body>
</html>
""".format(
    TITLE = 'CGI test',
    CSS = '', #css links
    BR = 'br />',
    COM = '<!--{}-->'.format(''),
    BODY=''
    )
print(html)


oh and using the same example to the OP, your essentially trying to do:
Code: Select all
html = """Content-type: text/html\n

<html>
    <head>
        <title>{TITLE}</title>
        {CSS}
        <script></script>
    </head>
<body>
{BODY}
</body>
</html>

print(html)

which is wrong. You start a triple quoted string but never terminate it.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: EOF while scanning triple-quoted string literal

Postby Crimson King » Tue Nov 19, 2013 1:44 am

Sorry for making it sound like you couldn't use triple quotes for non-docstring stuff.

I've never come across code like the one you posted metulburr, is it a common thing?
User avatar
Crimson King
 
Posts: 122
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: EOF while scanning triple-quoted string literal

Postby Mekire » Tue Nov 19, 2013 1:53 am

Some people, myself included, feel that triple quotes should only be used for docstrings, but it certainly does happen. I generally don't use them for any multiline strings in code because they preserve whitespace; which means you can't line them up nicely in your code without adding unwanted space in the actual string. If I need multiple line strings I am going to use parenthesis and implicit string concatenation. I would say in the case of things like metulburr's html, this would probably be better off saved in a different file and read into the script. I have seen similar in openGL code where shaders are included directly in scripts as strings. It isn't a good practice there either.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: EOF while scanning triple-quoted string literal

Postby metulburr » Tue Nov 19, 2013 2:16 am

I use triple quoted strings for mostly docstrings and commenting out multi-lined code temporarily. I am not sure how i feel about it using it in the context of html code though. However it seems the quickest way to write a single dynamic web page via cgi.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: EOF while scanning triple-quoted string literal

Postby Mekire » Tue Nov 19, 2013 2:20 am

Offtopic but the original issue has been addressed so...
metulburr wrote:I use triple quoted strings for [...] commenting out multi-lined code temporarily
Every IDE I have ever used (including IDLE) has a hotkey combination for commenting out lines; and another hotkey combination for uncommenting groups of lines that were commented out in the converse manner. Usually these comments are indicated by two # at the start of said lines.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: EOF while scanning triple-quoted string literal

Postby metulburr » Tue Nov 19, 2013 3:16 am

Every IDE I have ever used (including IDLE) has a hotkey combination for commenting out lines; and another hotkey combination for uncommenting groups of lines that were commented out in the converse manner. Usually these comments are indicated by two # at the start of said lines.

I actually never use those and use triple quotes instead. I am not sure why though.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: EOF while scanning triple-quoted string literal

Postby hrs » Tue Nov 19, 2013 10:23 am

There's also the problem that #!/usr/bin/env python3 is not on the first line. At least, my system requires it to be on the first line when running from a console. Maybe some IDEs are more lenient.
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: EOF while scanning triple-quoted string literal

Postby metulburr » Tue Nov 19, 2013 12:47 pm

the shebang line is for if your making the python file executable, running via:
Code: Select all
./script.py

instead of
Code: Select all
python<version> script.py


maybe your IDE is running via that, requiring the shebang every time. I have my IDE set to run
Code: Select all
python script.py

as i only want to put the shebang line on the ones that i specifically make executable and put into my /usr/bin/, for example.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: buran, W3C [Linkcheck] and 5 guests