Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function
#1
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)
Reply
#2
That's because lines 29 and 30 have 5's hard coded. Those should be replaced with gridSize.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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)
Reply
#4
Python is case sensitive. gridSize != Gridsize.
Reply


Forum Jump:

User Panel Messages

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