Posts: 33
Threads: 11
Joined: Aug 2019
Aug-14-2019, 01:31 PM
(This post was last modified: Aug-14-2019, 01:38 PM by Help_me_Please.)
Can I please have some help, I am passing in variables correctly and I am trying to check if the column is within the correct table (6 by 6) and I am returning board but the error I get is this:
line 29, in <module>
is_valid_location(board, column, invalid)
NameError: name 'board' is not defined
turn = 0
row = 6
columns = 6
invalid = True
game_over = False
def board_create(columns, row):
board = [[' '] * columns for row in range(row)]
return board
def drop_piece():
pass
def is_valid_location(board, column):
if column > 6 or column <= 0:
invalid = True
def get_next_open_row():
pass
board_create(columns, row)
while not game_over:
if turn == 0:
while invalid == True:
column = input("Player 1 make your selection (1-6):")
is_valid_location(board, column, invalid)
else:
while invalid == True:
column = input("Player 2 make your selection (1-6):")
is_valid_location(board, column, invalid)
turn = turn + 1
turn = turn % 2 Would appreciate some help.
Posts: 8,163
Threads: 160
Joined: Sep 2016
Aug-14-2019, 01:35 PM
(This post was last modified: Aug-14-2019, 01:36 PM by buran.)
well, with the code provided there are other errors, but not the one mentioned by you. Make sure you save the file before re-run it.
Posts: 1,950
Threads: 8
Joined: Jun 2018
It's unclear what is intended goal but I observe that there is function with two parameters (while parameter 'board' is not used in function body):
def is_valid_location(board, column): but you call it with three arguments:
is_valid_location(board, column, invalid)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 33
Threads: 11
Joined: Aug 2019
emended code:
turn = 0
row = 6
columns = 6
invalid = True
game_over = False
def board_create(columns, row):
board = [[' '] * columns for row in range(row)]
return board
def drop_piece():
pass
def is_valid_location(board, column, invalid):
if column > 6 or column <= 0:
invalid = True
def get_next_open_row():
pass
board_create(columns, row)
while not game_over:
if turn == 0:
while invalid == True:
column = input("Player 1 make your selection (1-6):")
is_valid_location(board, column, invalid)
else:
while invalid == True:
column = input("Player 2 make your selection (1-6):")
is_valid_location(board, column, invalid)
turn = turn + 1
turn = turn % 2
Posts: 1,950
Threads: 8
Joined: Jun 2018
I am afraid that I don't have enough spare time to guess what is the objective of this code.
Maybe you can state in spoken language what you want to accomplish?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 33
Threads: 11
Joined: Aug 2019
I want to create a 4 in a row game which I can adapt to make it harder, but I want to create a game board using a 2D array, I want to create a validation method by checking the column selected is within the correct parameters. I need to fix the error given.
Posts: 4,220
Threads: 97
Joined: Sep 2016
You never assign the return value of board_create. Line 23 should be board = board_create(columns, row) .
Posts: 1,358
Threads: 2
Joined: May 2019
Stop and plan a minute. Is board a local variable to a function, as you implement in is_valid_location() and board_create(), or is it global so it need not be passed? Row is defined as global in scope, but then used as both the counter and the limit in a for statement - line 8 is really ugly and scary and prone to stranger things. And, why does board_create() require any arguments when all of the information is global? By naming those arguments you just introduced all kinds of interesting scope problems that will kick you in the, well, yes.
wabba = 6
wabbawabba = 12
def abbaw(wabba, wabbawabba) :
wabba = wabbawabba
print(wabba, wabbawabba)
abbaw(wabba, wabbawabba)
12 12
print (wabba, wabbawabba)
6 12 So changing wabba in the function did not change the value in the global - same name but different namespace.
Stop. Think.
Posts: 1,950
Threads: 8
Joined: Jun 2018
Aug-14-2019, 07:55 PM
(This post was last modified: Aug-15-2019, 05:38 AM by perfringo.)
(Aug-14-2019, 03:41 PM)Help_me_Please Wrote: I want to create a 4 in a row game which I can adapt to make it harder, but I want to create a game board using a 2D array, I want to create a validation method by checking the column selected is within the correct parameters. I need to fix the error given.
I concur with jefsummers and suggest of doing some planning.
For me there is still ambiguity:
- what is '4 in a row game'?
- for what and how game board will be used?
- do you need check only columns (and not rows)?
EDIT:
Some additional observations to jefsummers: at any cost try to avoid this:
# code
columns = 6
# some more code
def is_valid_location(board, column):
if column > 6 or column <= 0:
invalid = True At some point in future you decide that board should have 8 columns instead of 6. You will change 'columns' value and expect your code to work same. But validation function doesn't know anything about columns. There is hardcoded 6 which is checked. As this is buried in body of function this bug will be hard to trace. Never hardcode.
For readability purposes you can instead of if column > 6 or column <=0: write if columns < column <= 0 .
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 33
Threads: 11
Joined: Aug 2019
Can someone please suggest what is wrong with the code so the 'player 2 make your suggestion' is show
turn = 0
invalid = True
game_over = False
def board_create():
board = [[' '] * column for row in range(row)]
def is_valid_location():
if (column-1) < column_choice < (column+1):
invalid = False
return invalid
else:
invalid = True
return invalid
row = int(input("How many rows: "))
column = int(input("How many columns: "))
board = board_create()
while not game_over:
if turn == 0:
while invalid == True:
column = str(column)
column_choice = int(input("Player 1 make your selection (1-"+column+"):"))
column = int(column)
is_valid_location()
else:
while invalid == True:
column = str(column)
column_choice = int(input("Player 2 make your selection (1-"+column+"):"))
column = int(column)
is_valid_location()
turn = turn + 1
turn = turn % 2
|