Python Forum
Usborne Coding for Beginners using Python 'Dodge the Bombs' Syntax Error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Usborne Coding for Beginners using Python 'Dodge the Bombs' Syntax Error
#1
I am working through a coding book with my child and we cannot figure out why we are getting errors. We have written other code using IDLE where we have imported tkinter and it has worked on our laptop.
Here is our code so far
 import tkinter
import random
gameOver = False
score = 0
SquaresToClear = 0
def play_bombdodger():
    create_bombfield(bombfield)
    window=tkinter.TK()
    layout_window(window)
    window.mainloop()
bombfield = []
def create_bombfield(bombfield):
    global squaresToClear
    for row in range(0,10):
        rowList=[]
        for column in range(0,10):
            if random.randint(1,100) < 20:
                rowList.append(1)
            else:
                rowList.append(0)
                squaresToClear = squaresToClear + 1
        bombfield.append(rowList)
    printfield(bombfield)
def printfield(bombfield):
    for rowList in bombfield:
        print(rowList)
def layout_window(window):
    for rowNumber, rowList in enumerate (bombfield):
        for columnNumber, columnEntry in enumerate (rowList):
            if random.randint(1,100)<25:
                square=tkinter.Label(Window, text="    ",bg= "darkgreen")
            elif random.randint(1,100)>75:
                square=tkinter.Label(window, text= "    ", bg = "seagreen")
            else:
                square = tkinter.Label(window,text="    ", bg="green")
            square.grid(row.grid(row=rowNumber,column=columnNumber)
            square.bind("<button-1>", on_click)
play_bombdodger()
Basically nothing is happening for us. We are not seeing a map of bombs in the shell window, and we are also not getting a square of green shades. We are reluctant to proceed further with making this game because of the errors.
Reply
#2
There are lots of errors if I try and run the code, see the comments
import tkinter # remove extra space indent
import random
gameOver = False
score = 0
squaresToClear = 0 # change capital S to lowrcase s
def play_bombdodger():
    create_bombfield(bombfield)
    window=tkinter.Tk() # change .TK to .Tk (lowercase k)
    layout_window(window)
    window.mainloop()
bombfield = []
def create_bombfield(bombfield):
    global squaresToClear
    for row in range(0,10):
        rowList=[]
        for column in range(0,10):
            if random.randint(1,100) < 20:
                rowList.append(1)
            else:
                rowList.append(0)
                squaresToClear = squaresToClear + 1
        bombfield.append(rowList)
    printfield(bombfield)
def printfield(bombfield):
    for rowList in bombfield:
        print(rowList)
def layout_window(window):
    for rowNumber, rowList in enumerate (bombfield):
        for columnNumber, columnEntry in enumerate (rowList):
            if random.randint(1,100)<25:
                square=tkinter.Label(window, text="    ",bg= "darkgreen") # change Window to lowercase window
            elif random.randint(1,100)>75:
                square=tkinter.Label(window, text= "    ", bg = "seagreen")
            else:
                square = tkinter.Label(window,text="    ", bg="green")
            square.grid(row.grid(row=rowNumber,column=columnNumber)) # add ) to the end, row is not defined
            square.bind("<button-1>", on_click) # on_click is not defined
play_bombdodger()
Reply
#3
Many thanks for your advice. We are completely new to coding and it is quite a different thing for me to try to work through with my son. We implemented your edits and it did run better. Still however, there is the issue that we are only seeing the initial list, and then a tk window pop up, but within the tk window there is not green shading of squares. After the nlank tk window pops up, nothing else happens. We have pressed on and typed out the rest of the code in the book, but it will not take us further than this blank tk window. Thank you very much for any help/advice offered. Here is the complete code
import tkinter
import random
gameOver = False
score = 0
squaresToClear = 0
def play_bombdodger():
    create_bombfield(bombfield)
    window=tkinter.Tk()
    layout_window(window)
    window.mainloop()
bombfield = []
def create_bombfield(bombfield):
    global squaresToClear
    for row in range(0,10):
        rowList=[]
        for column in range(0,10):
            if random.randint(1,100) < 20:
                rowList.append(1)
            else:
                rowList.append(0)
                squaresToClear = squaresToClear + 1
        bombfield.append(rowList)
    printfield(bombfield)
def printfield(bombfield):
    for rowList in bombfield:
        print(rowList)
def layout_window(window):
    for rowNumber, rowList in enumerate (bombfield):
        for columnNumber, columnEntry in enumerate (rowList):
            if random.randint(1,100)<25:
                square=tkinter.Label(window, text="    ",bg= "darkgreen")
            elif random.randint(1,100)>75:
                square=tkinter.Label(window, text= "    ", bg = "seagreen")
            else:
                square = tkinter.Label(window,text="    ", bg="green")
                square.grid(row.grid(row=rowNumber,column=columnNumber))
                square.bind("<button-1>",on_click)
def on_click(event):
    global score
    global gameOver
    global squaresToClear
    square = event.widget
    row = int(square.grid_info()["row"])
    column =int(square.grid_info()["column"])
    currentText=square.cget("text")
    if gameOver==False:
        if bombfield[row][column]==1:
            gameOver=True
            square.config(bg="red")
            print("Game Over! You hit a bomb!")
            print("Your score was:", score)
        elif currentText=="    ":
            square.config(bg="brown")
            totalBombs=0
            if row<9:
                if bombfield[row+1][column]==1:
                    totalBombs=totalBombs+1
            if row>0:
                if bombfield[row-1][column]==1:
                    totalBombs=totalBombs+1
            if column>0:
                if bombfield[row][column-1]==1:
                    totalBombs=totalBombs+1
            if column <9:
                if bombfield[row][column+1]==1:
                    totalBombs=totalBombs+1
            if row>0 and column>0:
                if bombfield[row-1][column-1]==1:
                    totalBombs=totalBombs+1
            if row<9 and column>0:
                if bombfield[row+1][column-1]==1:
                    totalBombs=totalBombs+1
            if row >0 and column <9:
                if bombfield[row-1][column+1]==1:
                    totalBombs=totalBombs+1
            if row <9 and column <9:
                if bombfield[row+1][column+1]==1:
                    totalBombs=totalBombs+1
            square.config(text = " " + str(totalBombs) + " ")
            squaresToClear = squaresToClear - 1
            score = score + 1
            if squaresToClear == 0:
                gameOver = True
                print("Well done! You found all the safe squares!")
                print("You score was:", score)
play_bombdodger()
Reply
#4
Hi CMSheWolfe,

I am also using the book to learn how to code, and got stuck with the "printfield" function. I wonder if it is what is causing you trouble too, so here is what I did to make it work:
import tkinter
import random

gameOver = False
score = 0
squaresToClear = 0

def play_bombdodger():
    create_bombfield(bombfield)
    window = tkinter.Tk()
    layout_window(window)
    window.mainloop()

bombfield = []
def create_bombfield(bombfield):
    global squaresToClear
    for row in range(0,10):
        rowList = []
        for column in range(0,10):
            if random.randint(1,100) < 20:
                rowList.append(1)
            else:
                rowList.append(0)
                squaresToClear = squaresToClear + 1
        bombfield.append(rowList)

def printfield(bombfield):
    for rowList in bombfield:
        print(rowList)

create_bombfield(bombfield)
printfield(bombfield)
- From what I can understand, you can't call a function if you didn't define it beforehand. So I put the "printfield(bombfield)" line under the function definition.
- Also, for it to work you need to call the create_bombfield function, so I have added that above the printfield function call.
- If this works ok and is showing you the "map of bombs" in your shell, you can add the # sign in front of the two function calls so they become comments and are not run with the rest of the programme...

#create_bombfield(bombfield)
#printfield(bombfield)
Reply
#5
Hi again,

I have just finished the programme and what I wrote previously was wrong, please ignore! (I didn't find a way to delete my previous post)
The good news is... I found what went wrong in your code!
import tkinter
import random
gameOver = False
score = 0
squaresToClear = 0
def play_bombdodger():
    create_bombfield(bombfield)
    window=tkinter.Tk()
    layout_window(window)
    window.mainloop()
bombfield = []
def create_bombfield(bombfield):
    global squaresToClear
    for row in range(0,10):
        rowList=[]
        for column in range(0,10):
            if random.randint(1,100) < 20:
                rowList.append(1)
            else:
                rowList.append(0)
                squaresToClear = squaresToClear + 1
        bombfield.append(rowList)
    printfield(bombfield)
def printfield(bombfield):
    for rowList in bombfield:
        print(rowList)
def layout_window(window):
    for rowNumber, rowList in enumerate (bombfield):
        for columnNumber, columnEntry in enumerate (rowList):
            if random.randint(1,100)<25:
                square=tkinter.Label(window, text="    ",bg= "darkgreen")
            elif random.randint(1,100)>75:
                square=tkinter.Label(window, text= "    ", bg = "seagreen")
            else:
                square = tkinter.Label(window,text="    ", bg="green")
                square.grid(row.grid(row=rowNumber,column=columnNumber)) #THIS NEEDS TO BE 1 TAB BACK TO ALIGN WITH IF/ELIF/ELSE, and remove the "row.grid()" to get square.grid(row=rowNumber,column=columnNumber)
                square.bind("<button-1>",on_click) #AS ABOVE,  #As above, and "Button" is with capital B
def on_click(event):
    global score
    global gameOver
    global squaresToClear
    square = event.widget
    row = int(square.grid_info()["row"]) 
    column =int(square.grid_info()["column"])
    currentText=square.cget("text")
    if gameOver==False:
        if bombfield[row][column]==1:
            gameOver=True
            square.config(bg="red")
            print("Game Over! You hit a bomb!")
            print("Your score was:", score)
        elif currentText=="    ":
            square.config(bg="brown")
            totalBombs=0
            if row<9:
                if bombfield[row+1][column]==1:
                    totalBombs=totalBombs+1
            if row>0:
                if bombfield[row-1][column]==1:
                    totalBombs=totalBombs+1
            if column>0:
                if bombfield[row][column-1]==1:
                    totalBombs=totalBombs+1
            if column <9:
                if bombfield[row][column+1]==1:
                    totalBombs=totalBombs+1
            if row>0 and column>0:
                if bombfield[row-1][column-1]==1:
                    totalBombs=totalBombs+1
            if row<9 and column>0:
                if bombfield[row+1][column-1]==1:
                    totalBombs=totalBombs+1
            if row >0 and column <9:
                if bombfield[row-1][column+1]==1:
                    totalBombs=totalBombs+1
            if row <9 and column <9:
                if bombfield[row+1][column+1]==1:
                    totalBombs=totalBombs+1
            square.config(text = " " + str(totalBombs) + " ")
            squaresToClear = squaresToClear - 1
            score = score + 1
            if squaresToClear == 0:
                gameOver = True
                print("Well done! You found all the safe squares!")
                print("You score was:", score)
play_bombdodger()
Let me know if this works!
Reply
#6
Blank lines between functions make code a lot easier to read.
import tkinter
import random

gameOver = False
score = 0
squaresToClear = 0
bombfield = []

def play_bombdodger():
    create_bombfield(bombfield)
    window=tkinter.Tk()
    layout_window(window)
    window.mainloop()

def create_bombfield(bombfield):
    global squaresToClear
    for row in range(0,10):
        rowList=[]
        for column in range(0,10):
            if random.randint(1,100) < 20:
                rowList.append(1)
            else:
                rowList.append(0)
                squaresToClear = squaresToClear + 1
        bombfield.append(rowList)
    printfield(bombfield)
    
def printfield(bombfield):
    for rowList in bombfield:
        print(rowList)
        
def layout_window(window):
    for rowNumber, rowList in enumerate (bombfield):
        for columnNumber, columnEntry in enumerate (rowList):
            if random.randint(1,100)<25:
                square=tkinter.Label(window, text="    ",bg= "darkgreen")
            elif random.randint(1,100)>75:
                square=tkinter.Label(window, text= "    ", bg = "seagreen")
            else:
                square = tkinter.Label(window,text="    ", bg="green")
            square.grid(row=rowNumber, column=columnNumber)  # Typo here
            square.bind("<Button-1>", on_click)  #capital "B"utton

def on_click(event):
    global score
    global gameOver
    global squaresToClear
    square = event.widget
    row = int(square.grid_info()["row"])
    column =int(square.grid_info()["column"])
    currentText=square.cget("text")
    if gameOver==False:
        if bombfield[row][column]==1:
            gameOver=True
            square.config(bg="red")
            print("Game Over! You hit a bomb!")
            print("Your score was:", score)
        elif currentText=="    ":
            square.config(bg="brown")
            totalBombs=0
            if row<9:
                if bombfield[row+1][column]==1:
                    totalBombs=totalBombs+1
            if row>0:
                if bombfield[row-1][column]==1:
                    totalBombs=totalBombs+1
            if column>0:
                if bombfield[row][column-1]==1:
                    totalBombs=totalBombs+1
            if column <9:
                if bombfield[row][column+1]==1:
                    totalBombs=totalBombs+1
            if row>0 and column>0:
                if bombfield[row-1][column-1]==1:
                    totalBombs=totalBombs+1
            if row<9 and column>0:
                if bombfield[row+1][column-1]==1:
                    totalBombs=totalBombs+1
            if row >0 and column <9:
                if bombfield[row-1][column+1]==1:
                    totalBombs=totalBombs+1
            if row <9 and column <9:
                if bombfield[row+1][column+1]==1:
                    totalBombs=totalBombs+1
            square.config(text = " " + str(totalBombs) + " ")
            squaresToClear = squaresToClear - 1
            score = score + 1
            if squaresToClear == 0:
                gameOver = True
                print("Well done! You found all the safe squares!")
                print("You score was:", score)

play_bombdodger()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Database Submit Entry Syntax Error Melford 27 7,506 Jan-27-2020, 04:20 PM
Last Post: Denni

Forum Jump:

User Panel Messages

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