Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
syntax error
#1
Hello,

I'm just starting coding and I had a syntax error that I don't understand. I'm copying from a book and I checked the code in different code checker online. I checked the indentation and I checked on the internet but I can't find an answer.

Here's the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#Memory
 
 
 
 
import random, pygame, sys
from pygame.locals import *
 
FPS = 30
WINDOWWIDTH = 640
WINDOWHEIGHT = 480
REVEALSPEED = 8
BOXSIZE = 40
GAPSIZE = 10
BOARDWIDTH = 10
BOARDHEIGHT = 7
assert(BOARDWIDTH * BOARDHEIGHT) % 2 == 0, 'Board needs to have an even number of boxes for pairs of matches.'
XMARGIN = int((WINDOWWIDTH - (BOARDWIDTH * (BOXSIZE + GAPSIZE))) / 2)
YMARGIN = int((WINDOWHEIGHT - (BOARDHEIGHT * (BOXSIZE + GAPSIZE))) / 2)
 
 
GRAY = ( 100, 100, 100)
NAVYBLUE = ( 60, 60, 100)
WHITE = ( 255, 255, 255)
RED = ( 255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
YELLOW = ( 255, 255, 0)
ORANGE = ( 255, 128, 0)
PURPLE = ( 255, 0, 255)
CYAN = ( 0, 255, 255)
 
BGCOLOR = NAVYBLUE
LIGHTBGCOLOR = GRAY
BOXCOLOR = WHITE
HIGHLIGHTCOLOR = BLUE
 
DONUT = 'donut'
SQUARE = 'square'
DIAMOND = 'diamond'
LINES = 'lines'
OVAL = 'oval'
 
ALLCOLORS = (RED, GREEN, BLUE, YELLOW, ORANGE, PURPLE, CYAN)
ALLSHAPES = (DONUT, SQUARE, DIAMOND, LINES, OVAL)
assert len(ALLCOLORS) * len(ALLSHAPES) * 2 >= BOARDWIDTH * BOARDHEIGHT, "Board is too big for the number of shapes/colors defined."
 
def main():
    global FPSCLOCK, DISPLAYSURF
    pygame.init()
    FPSCLOCK = pygame.time.Clock()
    DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
     
    mousex = 0
    mousey = 0
    pygame.display.set_caption('Memory Game')
    mainBoard = getRandomizedBoard()
    revealedBoxes = generateRevealedBoxesData(False)
     
    mainBoard = [[(DONUT, BLUE), (LINES, BLUE), (SQUARE, ORANGE)], [(SQUARE, GREEN), (DONUT, BLUE), (DIAMOND, YELLOW)], [(SQUARE, GREEN), (OVAL, YELLOW), (SQUARE, ORANGE)], [(DIAMOND, YELLOW), (LINES, BLUE), (OVAL, YELLOW)]]
    firstSelection = NONE
     
    DISPLAYSURF.fill(BGCOLOR)
    startGameAnimation(mainBoard)
     
    while TRUE:
        mouseClicked = False
         
        DISPLAYSURF.fill(BGCOLOR)
        drawBoard(mainBoard, revealedBoxes)
         
        for event in pygame.event.get():
            if event.type ==QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEMOTION:
                mousex, mousey = event.pos
            elif event.type == MOUSEBUTTONUP:
                mousex, mousey = event.pos
                mouseClicked = True
    boxx, boxy = getBoxAtPixel(mousex, mousey)
    if boxx != None and boxy !=None:
        if not revealedBoxes[boxx][boxy]:
            drawHighlightBox(boxx, boxy)
            if not revealedBoxes[boxx][boxy] and mouseClicked:
                revealBoxesAnimation(mainBoard,[(boxx, boxy)])
                revealedBoxes[boxx][boxy] = True
                 
                 
                if firstSelection == None:
                    firstSelection = (boxx, boxy)
                else:
                    icon1shape, icon1color = getShapeAndColor(mainBoard, firstSelection[0], firstSelection[1])
                    icon2shape,icon2color = getShapeAndColor(mainBoard, boxx, boxy)
                     
                    if iconshape != icon2shape or icon1color != icon2color:
                     
                        pygame.time.wait(1000)
                        coverBoxesAnimation(mainBoard,[(firstSelection[0], firstSelection[1]), (boxx, boxy)])
                        revealedBoxes[firstSelection[0]][firstSelection[1]] = False
                        revealedBoxes[boxx][boxy] = False
                     
                    elif hasWon(revealedBoxes):
                        gameWonAnimation(mainBoard)
                        pygame.time.wait(2000)
                         
                         
                        mainBoard = getRandomizedBoard()
                        revealedBoxes = generateRevealedboxesData(False)
                         
                         
                        drawBoard(mainBoard, revealedBoxes)
                        pygame.display.update()
                        pygame.time.wait(1000)
                         
                         
                        startGameAnimation(mainBoard)
                    firstSelection = None
                     
    pygame.display.update()
    FPSCLOCK.tick(FPS)
 
 
 
def generateRevealedBoxesData(val):
    revealedBoxes = []
    for i in range(BOARDWIDTH):
        revealedBoxes.append([val] * BOARDHEIGHT)
    return revealedBoxes
     
 
def getrandomizedBoard():
 
    icons = []
    for color in ALLCOLORS:
        for shape in ALLSHAPES:
            icons.append((shape, color))
    random.shuffle(icons)
    numIconsUsed = int(BOARDWIDTH * BOARDHEIGHT/2)
    icons = icons[:numIconsUsed] * 2
    random.shuffle(icons)
     
     
     
    board = []
    for x in range(BOARDWIDTH):
        column = []
        for y in range(BOARDHEIGHT):
            column.append(icons[0])
            del icons[0]
        board.append(column)
    return board
     
 
def splitIntoGroupsOf(groupSize, theList):
 
    result = []
    for i in range(0, len(theList), groupSize):
        result.append(theList[i:i + groupSize])
    return result
 
 
 
def leftTopCoordsOfBox(boxx, boxy):
 
    left = boxx * (BOXSIZE + GAPSIZE) + XMARGIN
    top = Boxy * (BOXSIZE + GAPSIZE) + YMARGIN
    return(left, top)
     
     
def getBoxAtPixel(x, y):
    for boxx in range(BOARDWIDTH):
        for boxy in range(BOARDHEIGHT):
            left, top = leftTopCoordsOfBox(boxx, boxy)
            boxRect = pygame.Rect(left, top, BOXSIZE, BOXESIZE)
            if boxRect.collidepoint(x, y):
                return (boxx, boxy)
    return (None, None)
     
     
def drawIcon(shape, color, boxx, boxy):
    quarter = int(BOXSIZE * 0.25)
    half = int(BOXSIZE * 0.5)
     
    left, top = leftTopCoordsOfBox(boxx, boxy)
     
    if shape == DONUT:
        pygame.draw.circle(DISPLAYSURF, color, (left + half, top+ half), half = 5)
        pygame.draw.circle(DISPLAYSURF, BGCOLOR, (left + half, top + half), quarter - 5)
    elif shape == SQUARE:
        pygame.draw.rect(DISPLAYSURF, color, (left + quarter, top + quarter, BOXSIZE - half, BOXSIZE - half))
    elif shape == DIAMOND:
        pygame.draw.polygone(DISPLAYSURF, color, ((left + half, top), (left + BOXSIZE - 1, top + half), (left + half, top + BOXSIZE - 1), (left, top + half)))
    elif shape == LINES:
        for i in range(0, BOXSIZE, 4):
            pygame.draw.line(DISPLAYSURF, color, (left, top + i), (left + i, top))
            pygame.draw.line(DISPLAYSURF, color, (left + i, top + BOXSIZE - 1), (Left + BOXSIZE - 1, top + i))
    elif shape == OVAL:
        pygame.draw.ellipse(DISPLAYSURF, color, (left, top + quarter, BOXSIZE, half))
         
         
def getShapeAndColor(board, boxx, boxy):
 
 
    return board[boxx][boxy[0], board[boxx][boxy][1]
     
 
def drawBoxCovers(board, boxes, coverage):
     
     
    for box in boxes:
        left, top = leftTopCoordsOfBox(box[0], box[1])
        pygame.draw.rect(DISPLAYSURF, BGCOLOR, (left, top, BOXSIZE, BOXSIZE))
        shape, color = getShapeAndColor(board, box[0], box[1])
        drawIcon(shape, color, box[0], box[1])
        if coverage > 0:
            pygame.draw.rect(DISPLAYSURF, BOXCOLOR, (left, top, coverage, BOXSIZE))
    pygame.display.update()
    FPSCLOCK.tick(FPS)
 
def revealBoxesAnimation(board, boxesToReveal):
 
    for coverage in range(BOXSIZE, (-REVEALSPEED) -1, -REVEALSPEED):
        drawBoxCovers(board, boxesToReveal, coverage)
 
def coverBoxesAnimation(board, boxesToCover):
 
    for coverage in range(0, BOXSIZE + REVEALSPEED, REVEALSPEED):
        drawBoxCovers(board, boxesToCover, coverage)   
     
def drawBoard(board, revealed):
 
    for boxx in range(BOARDWIDTH):
        for boxy in range(BOXHEIGHT):
            left, top = leftTopCoordsOfBox(boxx, boxy)
            if not revealed[boxx][boxy]:
                pygame.draw.rect(DISPLAYSURF, BOXCOLOR, (left, top, BOXSIZE, BOXSIZE))
            else:
                shape, color = getShapeAndColor(board, boxx, boxy)
                drawIcon(shape, color, boxx, boxy)
                 
def drawHighlightBox(boxx, boxy):
    left, top = leftTopCoordsOfBox(boxx, boxy)
    pygame.draw.rect(DISPLAYSURF, HIGHLIGHTCOLOR, (left -5, top - 5, BOXSIZE + 10, BOXSIZE + 10), 4)
     
def startgameAnimation(board):
 
    coveredBoxes = generateRevealedBoxesData(False)
    boxes = []
    for x in range(BOARDWIDTH):
        for y in range(BOARDHEIGHT);
            boxes.append((x, y))
    random.shuffle(boxes)
    boxeGroups = splitIntoGroupsOf(8, boxes)
    drawBoard(board, coveredBoxes)
    for boxgroup in boxGroups:
        revealBoxesAnimation(board, boxGroup)
        coverBoxesAnimation(board, boxgroup)
         
def gameWonAnimation(board):
 
    coveredBoxes = generateRevealedBoxesData(True)
    color1 = LIGHTBGCOLOR
    color2-BGCOLOR
     
    do i in range(13):
        color1, color2 = color2, color1
        DISPLAYSURF.fill(color1)
        drawBoard(board, coveredBoxes)
        pygame.display.update()
        pygame.time.wait(300)
         
def hasWon(revealedBoxes):
 
    for i in revealedBoxes:
        if False in i:
            return False
    return True
 
if __name__ == '__main__':
    main()
And this is the error I have

Error:
File "memory.py", line 208 def drawBoxCovers(board, boxes, coverage): ^ SyntaxError: invalid syntax
I'm working on Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux

Keep in mind that I have been coding for only a month

thanks for your time
Reply
#2
If you don't see any problem on that specific line, look at the code before it. In this case you don't have balanced brackets on the previous line.
Reply
#3
return board[boxx][boxy^[0], board[boxx][boxy][1]
Missing "]" where indicated.
Reply
#4
maybe you forgot a bracket in Line 205?

1
return board[boxx][boxy][0], board[boxx][boxy][1]
Reply
#5
Thanks! but I have another problem...

Error:
Traceback (most recent call last): File "memory.py", line 280, in <module> main() File "memory.py", line 64, in main startGameAnimation(mainBoard) File "memory.py", line 256, in startGameAnimation revealBoxesAnimation(board, boxGroup) File "memory.py", line 223, in revealBoxesAnimation drawBoxCovers(board, boxesToReveal, coverage) File "memory.py", line 213, in drawBoxCovers shape, color = getShapeAndColor(board, box[0], box[1]) File "memory.py", line 205, in getShapeAndColor return board[boxx][boxy][0], board[boxx][boxy][1] IndexError: list index out of range
I went through the code and can't find anything that's wrong???

Can you help again?
Reply
#6
Output:
return board[boxx][boxy][0], board[boxx][boxy][1] IndexError: list index out of range
I think the len of board[boxx][boxy] is < 2 or has no values
Reply
#7
I'm guessing that boxx or boxy are out of range. I would add some print statements to help find the error.
1
2
3
4
5
6
7
8
9
10
def getShapeAndColor(board, boxx, boxy):
    if boxx >= len(board) or boxy >= len(board[boxx]):
        print(f'getShapeAndColor(board, {boxx}, {boxy})')
        y_dimensions = [len(y) for y in board]
        print(f'board = {len(board)}x{y_dimensions}')
        return DONUT, BLUE
    if len(board[boxx][boxy]) < 2:
        print(f'getShapeAndColor(board, {boxx}, {boxy}) = {board[boxx][boxy]}')
        return DONUT, ORANGE
    return board[boxx][boxy][0], board[boxx][boxy][1]
Reply
#8
I added the code you suggested(which I don't totally comprehend) and I received this error in return:

Error:
getShapeAndColor(board, 1, 6) board = 4x[3, 3, 3, 3] Traceback (most recent call last): File "memory.py", line 289, in <module> main() File "memory.py", line 64, in main startGameAnimation(mainBoard) File "memory.py", line 265, in startGameAnimation revealBoxesAnimation(board, boxGroup) File "memory.py", line 232, in revealBoxesAnimation drawBoxCovers(board, boxesToReveal, coverage) File "memory.py", line 223, in drawBoxCovers drawIcon(shape, color, box[0], box[1]) File "memory.py", line 188, in drawIcon pygame.draw.circle(DISPLAYSURF, color, (left + half, top+ half), half = 5) TypeError: function missing required argument 'radius' (pos 4)
Now like I said, I've just been coding for a couple of months. My understanding is pretty basic and I don't know what to do with this new information:

getShapeAndColor(board, 1, 6)
board = 4x[3, 3, 3, 3]
Reply
#9
It means you have an error in your code.
1
print(f'getShapeAndColor(board, {boxx}, {boxy})')
is printing out
Output:
getShapeAndColor(board, 1, 6)
which means boxx == 1 and boxy == 6

We also know from this
Output:
board = 4x[3, 3, 3, 3]
That boxx can be in the range 0..3 but boxy can only be in the range 0..2.

Now you get to debug your code and find out where boxy is being set to an invalid index.

I am glad that Python provides a traceback when an exception is thrown, but for me at least the line that throws the exception is not where the error occurred. Most errors occur long before they result in something that causes your program to crash. I have a feeling that this is the case for your error. You'll have to follow the code backward, find where you are setting boxy, and determine how boxy is getting set to an invalid index.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  POST Syntax error amplay 0 642 Aug-07-2024, 02:43 PM
Last Post: amplay
  is this really a syntax error? Skaperen 4 1,603 May-25-2024, 07:31 AM
Last Post: snippsat
  World Clock syntax error OscarBoots 1 1,096 May-03-2024, 05:20 AM
Last Post: snippsat
  Syntax error for "root = Tk()" dlwaddel 15 6,362 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 1,292 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 4,870 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 2,981 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 2,626 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 2,728 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 1,822 Dec-05-2022, 06:38 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020