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)
@imoleayo97
Copy link

How do you determine your max rows and column

@ibevictory
Copy link

how does the max row and column function works

@tarkanbaba
Copy link

tarkanbaba commented Jan 2, 2021

so in your code i can only put the decided max values when i call the function but instead of doing this i want to give any value that i want when i call the function how do i do that ?

def board(rows,columns):
    for row in range(rows):
         if row % 2 == 0:
             for column in range(1,columns):
                 if column % 2 == 1:
                     if column != columns - 1  :
                         print(" ",end="")
                     else:
                         print(" ")
                 else:
                     print("|",end="")
         else:
             print("-"*columns)
    return True

board(24,24)

this is my code but it only works on even numbers, when i changed the values to odd numbers it wont print correctly. How do i make it work on odd numbers ?

@Rishabh5296
Copy link

Rishabh5296 commented Apr 23, 2021

I, Rishabh, pursuing "PYTHON IS EASY" got stuck on Homework Assignment #6. I wanna make the playing board with numbers, in which I want to use even no. as rows and odd no. as columns. Not this only, but I wanna change the row after 10th position/number and wanna change the column to the next column after every 10th number. I am stuck please help me making the shape I want.
Regards

Screenshot 2021-04-23 at 5 53 45 PM

@iamcarlp
Copy link

so in your code i can only put the decided max values when i call the function but instead of doing this i want to give any value that i want when i call the function how do i do that ?

def board(rows,columns):
    for row in range(rows):
         if row % 2 == 0:
             for column in range(1,columns):
                 if column % 2 == 1:
                     if column != columns - 1  :
                         print(" ",end="")
                     else:
                         print(" ")
                 else:
                     print("|",end="")
         else:
             print("-"*columns)
    return True

board(24,24)

this is my code but it only works on even numbers, when i changed the values to odd numbers it wont print correctly. How do i make it work on odd numbers ?

Hi,

for me, what i did is to put the validation if column != columns - 1 : before print("|",end="")

Hope it helps.

@kevinyongfy
Copy link

kevinyongfy commented Jun 30, 2021

so in your code i can only put the decided max values when i call the function but instead of doing this i want to give any value that i want when i call the function how do i do that ?

> def board(rows,columns):
>     for row in range(rows):
>          if row % 2 == 0:
>              for column in range(1,columns):
>                  if column % 2 == 1:
>                      if column != columns - 1  :
>                          print(" ",end="")
>                      else:
>                          print(" ")
>                  else:
>                      print("|",end="")
>          else:
>              print("-"*columns)
>     return True

board(24,24)

this is my code but it only works on even numbers, when i changed the values to odd numbers it wont print correctly. How do i make it work on odd numbers ?

This is what I am doing in order to work with odd numbers too:

def board(rows,columns):
for row in range(rows):
if row % 2 == 0:
for column in range(1,columns):
if column % 2 == 1:
if column != columns - 1 :
print(" ",end="")
else:
print(" ")
else:
if column != columns - 1 :
print("|",end="")
else:
print("|")

else:
print("-"*columns)
return True

Hope can help.

@201843052
Copy link

201843052 commented Jul 9, 2021

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!

@iamcarlp
Copy link

iamcarlp commented Jul 9, 2021

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)

@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