2

I'm trying to create a program that assigns whatever the person types in response to a certain prompt, it takes up more than two lines and I'm afraid that it's not recognizing the string because it is on separate lines. It keeps popping up with an "Incorrect Syntax" error and keeps pointing to the line below. Any way I can fix this?

given = raw_input("Is " + str(ans) + " your number?
Enter 'h' to indicate the guess is too high.
Enter 'l' to indicate the guess is too low.
Enter 'c' to indicate that I guessed correctly")
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
TrigonDark
  • 184
  • 2
  • 9

4 Answers4

6

You need to use multi-line strings, or else parentheses, to wrap a string in Python source code. Since your string is already within parentheses, I'd use that fact. The interpreter will automatically join strings together if they appear next to each other within parens, so you can rewrite your code like this:

given = raw_input("Is " + str(ans) + " your number?"
                  "Enter 'h' to indicate the guess is too high. "
                  "Enter 'l'to indicate the guess is too low. "
                  "Enter 'b' to indicate that I guessed correctly")

This is treated much as though there was a + between each of those strings. You could also write the plusses in yourself, but it's not necessary.

And as I alluded to above, you could also do it with triple-quoted strings (''' or """). But this (in my opinion) basically makes your code look awful, because of the indentation and newlines it imposes--I much prefer sticking with parentheses.

Henry Keiter
  • 16,863
  • 7
  • 51
  • 80
1

I would use multi-line strings, but you also have the following option:

>>> print "Hello world, how are you? \
... Foo bar!"
Hello world, how are you? Foo bar!

A backslash tells the interpreter to treat the following line as a continuation of the previous one. If you care how your code blocks look, you could append with +:

>>> print "Hello world, how are you? " + \
...       "Foo bar!"
Hello world, how are you? Foo bar!

Edit: as @moooeeeep stated, this escapes the newline character at the end of the statement. If you have any whitespace afterward, it'll screw everything up. So, I leave this answer up for reference only - I didn't know it worked as it does.

2rs2ts
  • 10,662
  • 10
  • 51
  • 95
  • 1
    the backslash actually escapes the following newline character. You must make sure that there are no more whitespace characters between the backslash and the newline in the source. Because of this, it is discouraged to use this technique. Rather use multiline strings or a parenthesized expression instead. – moooeeeep Jun 21 '13 at 22:39
  • Do you mean that it escapes the following newline character in the statement itself, or just in the string? I ask because I wouldn't expect that behavior in my second example (using `+`). – 2rs2ts Jun 21 '13 at 22:46
  • 1
    Actually, [I found the answer](http://docs.python.org/dev/howto/doanddont.html#using-backslash-to-continue-statements). I didn't know that... thank you! – 2rs2ts Jun 21 '13 at 22:52
0

Just use a multiline string. This way newlines in the string literal will be preserved (I assume this is what you want to achieve).

Example:

given = raw_input("""Is %s your number?
Enter 'h' to indicate the guess is too high.
Enter 'l' to indicate the guess is too low.
Enter 'c' to indicate that I guessed correctly""" % ans)
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
-1

You could also do triple quoted strings. The begin and end with """ and can span multiple lines.

James Thiele
  • 393
  • 3
  • 9
  • 2
    Triple-quoting can cause some annoying [indentation issues](http://stackoverflow.com/q/1412374/1014938); it might be worth expanding your answer to mention that. – Zero Piraeus Jun 21 '13 at 22:26