![]() |
Function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Function (/thread-23114.html) |
Function - gray1306 - Dec-11-2019 Hello, could someone let me know where am I going wrong with the below code please? The programs is supposed to ask the user for a number input to make a grid based on that number and an error message if an incorrect number is entered. But at the moment it only generates a 5x5 grid regardless of what is input, if someone could point out my error and help me resolve this that would be greatly appreciate. # Skeleton program for DrawGrid # define functions def GetGridSize(): validGrid = False while validGrid == False: print ("Please enter the size of the grid(max 20):") thisGridsize = int(input()) if (thisGridsize > 0) and (thisGridsize <=20): validGrid = True return thisGridsize def GetGridRow(aRowLength): # draws a single row using |_ for each square thisRow = '|_' * (aRowLength) # add closing | to row thisRow = thisRow + '|' return thisRow def DisplayGrid(aGridSize, aRow): # display top of grid using _ as top of each square print(' _' * aGridSize) # display rows of |_| for each row for rowCount in range(aGridSize): print(aRow) # main program gridSize = GetGridSize () rowToDraw = GetGridRow(5) DisplayGrid(5, rowToDraw) RE: Function - ichabod801 - Dec-11-2019 That's because lines 29 and 30 have 5's hard coded. Those should be replaced with gridSize. RE: Function - gray1306 - Dec-11-2019 Thank you so now I have this and have this error - line 31, in <module> rowToDraw = GetGridRow(Gridsize) NameError: name 'Gridsize' is not defined what does it mean? sorry if I sound a bit thick # Teaching students to code: Python 2 # Reflect activity - Task 1 # Skeleton program for DrawGrid # define functions def GetGridSize(): validGrid = False while validGrid == False: print ("Please enter the size of the grid(max 20):") thisGridsize = int(input()) if (thisGridsize > 0) and (thisGridsize <=20): validGrid = True return thisGridsize def GetGridRow(aRowLength): # draws a single row using |_ for each square thisRow = '|_' * (aRowLength) # add closing | to row thisRow = thisRow + '|' return thisRow def DisplayGrid(aGridSize, aRow): # display top of grid using _ as top of each square print(' _' * aGridSize) # display rows of |_| for each row for rowCount in range(aGridSize): print(aRow) # main program gridSize = GetGridSize () rowToDraw = GetGridRow(Gridsize) DisplayGrid (Gridsize, rowToDraw) RE: Function - stullis - Dec-11-2019 Python is case sensitive. gridSize != Gridsize. |