Skip to content

Instantly share code, notes, and snippets.

@qmmr
Last active July 29, 2021 04:29
Show Gist options
  • Save qmmr/ace1346a55101a7cc4f6df9fe24c945e to your computer and use it in GitHub Desktop.
Save qmmr/ace1346a55101a7cc4f6df9fe24c945e to your computer and use it in GitHub Desktop.
Python Homework Assignment #6: Advanced Loops
"""
pirple.com/python
Python Homework Assignment #6: Advanced Loops
Details:
Create a function that takes in two parameters: rows, and columns, both of which are integers.
The function should then proceed to draw a playing board (as in the examples from the lectures)
the same number of rows and columns as specified. After drawing the board, your function should return True.
Extra Credit:
Try to determine the maximum width and height that your terminal and screen can comfortably fit without wrapping.
If someone passes a value greater than either maximum, your function should return Talse.
"""
def create_board(rows, columns):
max_columns = 130
max_rows = 11
rows = int(rows)
columns = int(columns)
if columns <= max_columns and rows <= max_rows:
for row in range(rows):
if row % 2 == 0:
for col in range(1, columns):
if col % 2 == 1:
if col != columns - 1:
print(" ", end="")
else:
print(" ")
else:
print("|", end="")
else:
print("-" * columns)
# After drawing is done return True
return True
else:
# Return False when matrix is not fitting the screen
reason = ""
if columns > max_columns and rows > max_rows:
reason = "columns and rows"
elif columns > max_columns:
reason += "columns"
elif rows > max_rows:
reason += "rows"
print("Sorry, cannot create the board, too many {0:s}.".format(reason))
return False
create_board(11, 130)
@MAN4647
Copy link

MAN4647 commented Jul 29, 2021

you can simply put one more if condition
def create_board(rows, columns):
max_columns = 100
max_rows = 100
rows = int(rows)
columns = int(columns)
if columns <= max_columns and rows <= max_rows:
for row in range(rows):
if row % 2 == 0:
if columns % 2 == 0: # if column value is even
for col in range(1, columns):
if col % 2 == 1:
if col != columns - 1:
print(" ", end="")
else:
print(" ")
else:
print("|", end="")
else: # if column value is odd
for col in range(1, columns):
if col % 2 == 0:
if col != columns - 1:
print(" ", end="")
else:
print(" ")
else:
print("|", end="")
else:
print("-" * (columns-2))
# After drawing is done return True
return True
else:
print("wrong input")
return False

create_board(10,15)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment