Below is all the coding. At the last section where the functions interact with one another the print welcome to tic tac toe and which player goes first does not print. How and why? Indents are actually as they should be, bringing it over to stackoverflow did not bring over the indents. All functions work. Don't have an issue with them. Just want to understand why the last cell the two print statements welcome to tic tac toe and who goes first does not work at all. restarted kernel retyped them, deleted the cell copy and pasted. no clue how to make it print.
from IPython.display import clear_output
def run_board(board):
clear_output()
print(' '+board[7]+' | '+board[8]+' | '+board[9]+' ')
print(' ~ ~ ~')
print(' '+board[4]+' | '+board[5]+' | '+board[6]+' ')
print(' ~ ~ ~')
print(' '+board[1]+' | '+board[2]+' | '+board[3]+' ')
def choose_marker():
marker=''
while not(marker=='X' or marker=='O'):
marker = input('Player 1 choose X or O ').upper()
if marker=='X':
return('X','O')
else:
return('O','X')
def place_marker(board,marker,position):
board[position]=marker
def win_check(board,marker):
return ((board[1]==marker and board[2]==marker and board[3]==marker)or
(board[4]==marker and board[5]==marker and board[6]==marker)or
(board[7]==marker and board[8]==marker and board[9]==marker)or
(board[7]==marker and board[4]==marker and board[1]==marker)or
(board[8]==marker and board[5]==marker and board[2]==marker)or
(board[9]==marker and board[6]==marker and board[3]==maker)or
(board[7]==marker and board[5]==marker and board[3]==marker)or
(board[9]==marker and board[5]==marker and board[1]==marker))
def spot_check(board,position):
return board[position]==' '
def full_board_check(board):
for pos in range(1,10):
if spot_check(board,pos):
return False
return True
def pick_spot(board):
position=0
while position not in [1,2,3,4,5,6,7,8,9] or not spot_check(board,position):
position= int(input('Pick a location per num keys far right.'))
return position
import random
def go_first():
if random.randint(0,1)==0:
return 'Player 1'
else:
return 'Player 2'
def play_again():
return input('Do you want to play again? Yes or No.').lower().startswith('y')
now the section Where it all goes into one cell and the print does not work:
print('Welcome to Tic Tac Toe')
while True:
game_board=[' ']*10
turn=go_first()
print(turn+' Will go first')
run_board(game_board)
player1,player2=choose_marker()
play_game=input('Are you Ready to play?').lower()
if play_game[0]=='y':
game_on=True
else:
game_on=False
while game_on:
if turn=='Player 1':
run_board(game_board)
position=pick_spot(game_board)
place_marker(game_board,player1,position)
if win_check(game_board,player1):
run_board(game_board)
print('You won MF')
game_on=False
else:
if full_board_check(game_board):
run_board(game_board)
print('Wow you both lost. Nice.')
game_on=False
else:
turn='Player 2'
elif turn=='Player 2':
run_board(game_board)
position=pick_spot(game_board)
place_marker(game_board,player2,position)
if win_check(game_board,player2):
run_board(game_board)
print('You won even though you went last')
game_on=False
else:
if full_board_check(game_board):
run_board(game_board)
print('Wow you both lost')
game_on=False
else:
turn='Player 1'
if not play_again():
break
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…