Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

python - print only works when it wants too. I need to know why

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It does print what you want it to, but then you clear it away. For example it prints Welcome to Tic Tac Toe but then you run run_board which clears it away. I deleted run_board from the initial while loop and that solved the problem you originally posted about.

However, I ran into 2 other minor issues. I fixed those and posted it below. Don't look at the code below if you are doing this for learning and want to try solving it yourself first, but it's there if you want it.

print('Welcome to Tic Tac Toe')
game_on=False
while game_on==False:
    game_board=[' ']*10
    turn=go_first()
    print(turn+' Will go first')
    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 game_on==False:
        if not play_again():
            break

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...