Python Forum
Simple Function Problem, please help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Function Problem, please help
#11
I would do this completely differently:
import copy

num_rows = 6
num_cols = 5
board = [ ]

def make_board(num_rows, num_cols):
    newrow = [ ]
    for row in range(num_rows):
        for col in range(num_cols):
            newrow.append(0)
        board.append(newrow)
        newrow = [ ]

def drawboard():
    border = ('----' * num_cols) + '-'
    for row in range(num_rows):
        print(border)
        for col in range(num_cols):
            # print(f'row: {row}, col: {col}')
            if board[row][col] == 1:
                cell = '| x '
            else:
                cell = '|   '
            print(cell, end='')
        print('|')
    print(border)

def set_cell(row, col):
    # one based, to make zero based, use:
    # board[row][col] = 1
    board[row-1][col-1] = 1

def clear_cell(row, col):
    # one based, to make zero based, use:
    # board[row][col] = 1
    board[row-1][col-1] = 0

def columncheck(row):
    sums = 0
    for col in range(num_cols):
        sums += board[row-1][col]
    return sums == num_cols

def show_results(row):
    drawboard()
    if columncheck(row):
        print('row {} is full\n'.format(row))
    else:
        print('row {} is not full\n'.format(row))

def test_it():
    make_board(num_rows, num_cols)
    # Set first 4 cells of row 6
    row = 6
    for col in range(4):
        set_cell(row, col+1)
    # Now set last cell
    show_results(row)
    set_cell(row, 5)
    show_results(row)

if __name__ == '__main__':
    test_it()
run results:
Output:
--------------------- | | | | | | --------------------- | | | | | | --------------------- | | | | | | --------------------- | | | | | | --------------------- | | | | | | --------------------- | x | x | x | x | | --------------------- row 6 is not full --------------------- | | | | | | --------------------- | | | | | | --------------------- | | | | | | --------------------- | | | | | | --------------------- | | | | | | --------------------- | x | x | x | x | x | --------------------- row 6 is full
Reply


Messages In This Thread
RE: Simple Function Problem, please help - by mpd - Dec-28-2017, 02:43 AM
RE: Simple Function Problem, please help - by mpd - Jan-02-2018, 01:50 PM
RE: Simple Function Problem, please help - by Larz60+ - Jan-01-2018, 12:51 AM
RE: Simple Function Problem, please help - by mpd - Jan-02-2018, 11:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  A simple problem, how best to solve it? SuchUmami 2 762 Sep-01-2023, 05:36 AM
Last Post: Pedroski55
  How to solve this simple problem? Check if cvs first element is the same in each row? thesquid 2 1,296 Jun-14-2022, 08:35 PM
Last Post: thesquid
Big Grin question about simple algorithm to my problem jamie_01 1 1,724 Oct-04-2021, 11:55 AM
Last Post: deanhystad
  simple login problem MMOToaster 2 2,450 Feb-25-2020, 09:28 AM
Last Post: MMOToaster
  got SyntaxError while building simple function zarize 2 2,179 Feb-14-2020, 10:51 AM
Last Post: zarize
  Problem with simple 3D Vektor calculation Pythocodras 0 1,742 Dec-11-2019, 07:18 PM
Last Post: Pythocodras
  Simple statistics with range function Pythonlearner2019 2 2,177 Nov-25-2019, 05:25 PM
Last Post: Pythonlearner2019
  Simple problem. looking for an efficient way silverchicken24 3 2,394 Oct-14-2019, 07:13 PM
Last Post: Larz60+
  Cannot get simple i/o to function. jerryi 10 6,813 Jul-27-2019, 06:22 PM
Last Post: jerryi
  simple string & input problem kungshamji 5 3,745 Jun-23-2019, 03:54 PM
Last Post: kungshamji

Forum Jump:

User Panel Messages

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