Python Forum

Full Version: Function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
That's because lines 29 and 30 have 5's hard coded. Those should be replaced with gridSize.
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)
Python is case sensitive. gridSize != Gridsize.