Reputation: 1
I am having trouble correcting the output for this code:
print('--------')
print('|'+board[7]+ '|' +board[8] +'|' +board[9]+'|')
print('--------')
print('|' +board[4]+ '|'+board[5]+'|' +board[6]+'|')
print('--------')
print('|' +board[1]+ '|'+board[2]+'|' +board[3]+'|')
print('--------')
real_board=['#','#','#','#','#','#','#','#','#','#']
def xchecker_tool(board, mark):
vertical=print(f'congratulations player {mark}! you have won by lining up vertically on the board!')
horizontal=print(f'congratulations player {mark}! you have won by lining up horizontally on the board!')
diagonal= print(f'congratulations player {mark}! you have won by lining up diagonally!')
if board[1]==mark and board[2]==mark and board[3]==mark:
horizontal
elif board[4]==mark and board[5]==mark and board[6]==mark:
horizontal
elif board[7]==mark and board[8]==mark and board[9]==mark:
horizontal
if board[1]==mark and board[4]==mark and board[7]==mark:
vertical
elif board[2]==mark and board[5]==mark and board[8]==mark:
vertical
elif board[3]==mark and board[6]==mark and board[9]==mark:
vertical
if board[1]==mark and board[5]==mark and board[9]==mark:
diagonal
elif board[3]==mark and board[5]==mark and board[7]==mark:
diagonal
def game_rounds(round):
player_moves=int(input('choose a number from 1 to 9: '))
while True:
if (player_moves)<1 or (player_moves)>9:
print('unacceptable range')
elif (player_moves)==1:
real_board[1]='X'
elif (player_moves)==2:
real_board[2]='X'
elif (player_moves)==3:
real_board[3]='X'
elif (player_moves)==4:
real_board[4]='X'
elif (player_moves)==5:
real_board[5]='X'
elif (player_moves)==6:
real_board[6]='X'
elif (player_moves)==7:
real_board[7]='X'
elif (player_moves)==8:
real_board[8]='X'
else:
real_board[9]='X'
break
display_board(real_board)
def y_rounds(round):
y_moves=int(input('choose a number from 1 to 9: '))
while True:
if (y_moves)<1 or (y_moves)>9:
print('unacceptable range')
elif (y_moves)==1:
real_board[1]='O'
elif (y_moves)==2:
real_board[2]='O'
elif (y_moves)==3:
real_board[3]='O'
elif (y_moves)==4:
real_board[4]='O'
elif (y_moves)==5:
real_board[5]='O'
elif (y_moves)==6:
real_board[6]='O'
elif (y_moves)==7:
real_board[7]='O'
elif (y_moves)==8:
real_board[8]='O'
else:
real_board[9]='O'
break
display_board(real_board)
def run_game(full):
game_on=True
game_off=False
t=0
while game_on:
game_rounds(1)
t+=1
if t==5:
break
y_rounds(1)
xchecker_tool(real_board, 'X')
xchecker_tool(real_board, 'O')
run_game(1)
I am getting this output. From my understanding, the xchecker_tool function isn't defined properly which is why it prints the 'congrats' message every two turns even when 'x' or 'o' isn't lined up. I'd also like help in cleaning up the script as it's very long-winded but cannot do this at my current level because of recurrent errors.
choose a number from 1 to 9: 3
--------
|#|#|#|
--------
|#|#|#|
--------
|#|#|X|
--------
choose a number from 1 to 9: 5
--------
|#|#|#|
--------
|#|O|#|
--------
|#|#|X|
--------
congratulations player X! you have won by lining up vertically on the board!
congratulations player X! you have won by lining up horizontally on the board!
congratulations player X! you have won by lining up diagonally!
congratulations player O! you have won by lining up vertically on the board!
congratulations player O! you have won by lining up horizontally on the board!
congratulations player O! you have won by lining up diagonally!
Upvotes: 0
Views: 50
Reputation: 77837
I recommend that you back off: don't try to write the entire program at once. Your programming skills are not yet up to this level of complexity. I don't write this much code at once. Instead, write a block of code, debug it, and then go on. Wherever you can, isolate the parts of code while you debug them.
For instance, hard-code a few test boards for your xchecker_tool
; work on that until you can properly identify the winner/loser for each test-game situation. You'll quickly see a problem: you don't presently understand how print
works.
vertical=print(f'congratulations player {mark}! you have won by lining up vertically on the board!')
This statement prints a message and returns the value None
, which you assign to vertical
. You do the same with two other variables. You can easily discover this with a tracing statement:
print(vertical, horizontal, diagonal)
See this lovely debug blog for help. Insert useful output statements to trace the control and data flow.
Your variables are merely value storage, not a way to pre-define a decision process.
See how this works? When you hit a problem point, work through the applicable tutorial again. Use that immediately to correct your most recent block of code.
Upvotes: 0
Reputation: 44838
Here:
vertical=print(f'congratulations player {mark}! you have won by lining up vertically on the board!')
horizontal=print(f'congratulations player {mark}! you have won by lining up horizontally on the board!')
diagonal= print(f'congratulations player {mark}! you have won by lining up diagonally!')
print
will be executed, and its return value will be bound to the variables. You define three variables - and thus print
will be executed three times, outputting text to the terminal.
Code like this:
if board[1]==mark and board[2]==mark and board[3]==mark:
horizontal
Merely "mentions" the variable horizontal
, which doesn't really execute anything. You could've as well written:
if board[1]==mark and board[2]==mark and board[3]==mark:
None # won't do anything
... # won't do anything either
print
board[1]
None of those will execute anything.
You should print in the if
statements:
if board[1]==mark and board[2]==mark and board[3]==mark:
print(f'congratulations player {mark}! you have won by lining up horizontally on the board!')
# and so on for the other `if`s
Upvotes: 1