-
-
Save qmmr/ace1346a55101a7cc4f6df9fe24c945e to your computer and use it in GitHub Desktop.
""" | |
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) |
def drawPlayBoard(row,col): evenRow="-"*(2*col-1) oddRow="" for i in range(2*col-1): if col==1: #to deal with modulo 0 problem oddRow+=" " elif i%(2*col-2)==0: #first and last of oddRow will be spaces oddRow+=" " elif i%2==0: #even index of oddRow will be space oddRow+=" " else: #odd index of oddRow will be "|" oddRow+="|" for i in range(2*row-1): #alternately print oddRow and evenRow if i%2==0: print(oddRow) else: print(evenRow) return True drawPlayBoard(3,4)
This is how I approached this homework without using nested loops, albeit the title "advanced loops" :). Looking at the details, I interpreted the number of columns and rows as the dimension of the matrix where each entry is the " " (space). Although I am still not sure how to determine the max number of cols and rows... Any criticism is welcomed!
For me, i manually count letters as I type to determine the max(until it goes to the next line)
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)
This is how I approached this homework without using nested loops, albeit the title "advanced loops" :). Looking at the details, I interpreted the number of columns and rows as the dimension of the matrix where each entry is the " " (space). Although I am still not sure how to determine the max number of cols and rows... Any criticism is welcomed!