Question: Functions can be used to replace multiple code statements. This occurs in the Tic - Tac - Toe game program within the tutorial 2 .

Functions can be used to replace multiple code statements. This occurs in the Tic-Tac-Toe game program within the tutorial 2.3.5 Finishing the Tic-Tac-Toe Program; see the printBoard function created there as an example.
What if we wanted to replace the following lines in the Tic-Tac-Toe game:
while (row <1 or row >3):
row = int(input(playerTurn +" player, select a row 1-3: "))
if (row <1 or row >3):
print("The row must be between 1 and 3.")
with a function called turn. For the call to this function, we could use:
row = turn("row", playerTurn)
where we input into the function that we want to select a row ("row") and which player is making this selection (playerTurn).
Which code segment properly implements this function?
def turn(col_row, player):
good_selection = True
while (good_selection == False):
col_row_picked = int(input(playerTurn +" player, select a {}1-3: ".format(col_row)))
if (col_row_picked <1 or col_row_picked >3):
print("The {} must be between 1 and 3.".format(col_row))
else:
good_selection = True
return col_row
def turn(col_row, player):
good_selection = True
while (good_selection == False):
col_row_picked = int(input(playerTurn +" player, select a {}1-3: ".format(col_row)))
if (col_row_picked <1 or col_row_picked >3):
print("The {} must be between 1 and 3.".format(col_row))
else:
good_selection = True
return col_row_picked
def turn(col_row, player):
good_selection = False
while (good_selection == False):
col_row_picked = int(input(playerTurn +" player, select a {}1-3: ".format(col_row)))
if (col_row_picked <1 or col_row_picked >3):
print("The {} must be between 1 and 3.".format(col_row))
else:
good_selection = True
return col_row
def turn(col_row, player):
good_selection = False
while (good_selection == False):
col_row_picked = int(input(playerTurn +" player, select a {}1-3: ".format(col_row)))
if (col_row_picked <1 or col_row_picked >3):
print("The {} must be between 1 and 3.".format(col_row))
else:
good_selection = True
return col_row_picked

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!