0

I'm trying to build a Tetris game in python and I am getting the above error relating to this piece of code. I have tried taking out anything up to the slash but its still not working. Can anyone help a newbie out?

# SHAPE FORMATS

S = [[\'.....\',
    \'.....\',
    \'..00.\',
    \'.00..\',
    \'.....\'],
    [\'.....\',
    \'..0..\',
    \'..00.\',
    \'...0.\',
    \'.....\']]
Rayees AC
  • 4,426
  • 3
  • 8
  • 31
  • I don't understand what you are trying to initialize `S` to. I suspect the backslashes are completely unnecessary. – Dave Costa Sep 28 '20 at 16:51
  • How do you want your string `S`? Can you show it without `\\`, exactly how you want them? – Shivam Jha Sep 28 '20 at 16:53
  • Related: use triple-quotes for multiline strings, and use [this trick to start the first line with `"""\` so you don't get unwanted leading newline](https://stackoverflow.com/a/57464608/202229). – smci Sep 28 '20 at 17:04
  • You can accept one answer (if it helps you) by [clicking on the big gray check button on its left side](https://meta.stackoverflow.com/questions/354584/teaching-new-users-how-to-accept-an-answer). If you wish you can also upvote helpful answers by clicking on the upper gray triangle. – smci Jan 05 '21 at 11:44

3 Answers3

0

There's no need to escape the single quotes in your example.

Try:

S = [['.....',
    '.....',
    '..00.',
    '.00..',
    '.....'],
    ['.....',
    '..0..',
    '..00.',
    '...0.',
    '.....']]

jim washer
  • 73
  • 7
  • I noticed others posted a comment at about the same time I posted my "answer". I'm fairly new here and have only recently broken the 50pt level that allows me to post comments. How does one decide whether to post an answer or a comment? Are there rules/suggestions about this? – jim washer Sep 28 '20 at 17:01
  • 1
    jim_washer: Yeah it's a judgment call: for a trivial answer that could be given in a one-line comment, especially where the question is likely to get closed, can post the comment, or both comment and answer. For an answer that would be a duplicate of existing Q&A on SO, we don't even post an answer, we close-as-duplicate. – smci Sep 28 '20 at 17:39
0

the character ' is used to denote a string,

s = 'this is a string'

In order to use the character ' within a string, they can be proceeded by a \

s = 'this string has a \' in it'

judging by your use of \, you want each line to have a ' on either side. That means you still need a set of ''s to encase the actual string:

S = [['\'.....\'',
    '\'.....\'',
    '\'..00.\'',
    '\'.00..\'',
    '\'.....\''],
    ['\'.....\'',
    '\'..0..\'',
    '\'..00.\'',
    '\'...0.\'',
    '\'.....\'']]

if instead you just want lines without ' on either side of each line, it would look like this:

S = [['.....',
    '.....',
    '..00.',
    '.00..',
    '.....'],
    ['.....',
    '..0..',
    '..00.',
    '...0.',
    '.....']]
Warlax56
  • 1,170
  • 5
  • 30
0

You don't need to escape your single-quotes. You can use triple-quotes for multiline strings, and use this trick to start the first line with """\ so you don't get unwanted leading newline.

Then you can use .split() to split the two strings into two sublists.

S = [ """\
.....
.....
..00.
.00..
.....""".split(),

"""\
.....
..0..
..00.
...0.
.....""".split() ]
smci
  • 32,567
  • 20
  • 113
  • 146