patternpythonMinor
Creating a grid to the specified size using lists
Viewed 0 times
thegridcreatingsizeusinglistsspecified
Problem
My code creates a "grid" of empty places, where 0 is used as the place holder, in a size specified by the user.
I was wondering if there were more efficient ways of doing this,or perhaps ways that make more aesthetically pleasing grids with less code.
My code is in working order and does as it says on the label, I also implemented a system that does not allow for incorrect inputs.
All feedback welcome!
I was wondering if there were more efficient ways of doing this,or perhaps ways that make more aesthetically pleasing grids with less code.
My code is in working order and does as it says on the label, I also implemented a system that does not allow for incorrect inputs.
def gridSize_def():
global gridSize
while True:
try:
gridSize = int(input("input grid length and width\n"))
if gridSize<2:
print ("invalid int")
else:
break
except ValueError:
print ("invalid input")
grids = [[] for _ in range (gridSize)]
for i in range(0,gridSize):
grids[i-1]=[0 for _ in range(gridSize)]
return grids
def grid_def():
for i in range (0,gridSize):
print (grids[i-1])
grids = gridSize_def()
grid_def()All feedback welcome!
Solution
Grid creation
You know about list-comprehensions, but you use a default value before filling your rows with zeroes. You could do it in one go:
which can be simplified to
Grid printing
The
Function parameters
It is bad practice to rely on
This let you also extract input handling from your grid creation function which is also better as let you have a single responsibility per function:
Exception handling
Python have
You know about list-comprehensions, but you use a default value before filling your rows with zeroes. You could do it in one go:
grids = [[0 for _ in range(gridSize)] for _ in range(gridSize)]which can be simplified to
grids = [[0] * gridSize for _ in range(gridSize)]Grid printing
The
for loop in Python is meant to be used to iterate directly over elements rather than indices:for row in grids:
print(row)Function parameters
It is bad practice to rely on
global to share data accros functions. You should define those function to accept parameters instead.This let you also extract input handling from your grid creation function which is also better as let you have a single responsibility per function:
def ask_size():
while True:
try:
grid_size = int(input("input grid length and width\n"))
if grid_size < 2:
print ("invalid int")
else:
return grid_size
except ValueError:
print ("invalid input")
def make_grid(size):
return [[0] * size for _ in range(size)]
def print_grid(grid):
for row in grid:
print(row)
grids = make_grid(ask_size())
print_grid(grids)Exception handling
Python have
else clauses that are executed when the try part succeeded. The aim is to reduce the amount of code protected by the try to avoid catching unexpected exceptions. In your case, you could change that part to:def ask_size():
while True:
try:
grid_size = int(input("input grid length and width\n"))
except ValueError:
print ("invalid input")
else:
if grid_size > 1:
return grid_size
print ("invalid int")Code Snippets
grids = [[0 for _ in range(gridSize)] for _ in range(gridSize)]grids = [[0] * gridSize for _ in range(gridSize)]for row in grids:
print(row)def ask_size():
while True:
try:
grid_size = int(input("input grid length and width\n"))
if grid_size < 2:
print ("invalid int")
else:
return grid_size
except ValueError:
print ("invalid input")
def make_grid(size):
return [[0] * size for _ in range(size)]
def print_grid(grid):
for row in grid:
print(row)
grids = make_grid(ask_size())
print_grid(grids)def ask_size():
while True:
try:
grid_size = int(input("input grid length and width\n"))
except ValueError:
print ("invalid input")
else:
if grid_size > 1:
return grid_size
print ("invalid int")Context
StackExchange Code Review Q#134647, answer score: 7
Revisions (0)
No revisions yet.