![]() |
Usborne Coding for Beginners using Python 'Dodge the Bombs' Syntax Error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Usborne Coding for Beginners using Python 'Dodge the Bombs' Syntax Error (/thread-27052.html) |
Usborne Coding for Beginners using Python 'Dodge the Bombs' Syntax Error - CMSheWolfe - May-24-2020 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. RE: Usborne Coding for Beginners using Python 'Dodge the Bombs' Syntax Error - Yoriz - May-24-2020 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() RE: Usborne Coding for Beginners using Python 'Dodge the Bombs' Syntax Error - CMSheWolfe - May-24-2020 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() RE: Usborne Coding for Beginners using Python 'Dodge the Bombs' Syntax Error - Yokiya - Jun-18-2020 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) RE: Usborne Coding for Beginners using Python 'Dodge the Bombs' Syntax Error - Yokiya - Jun-18-2020 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! RE: Usborne Coding for Beginners using Python 'Dodge the Bombs' Syntax Error - deanhystad - Jun-18-2020 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() |