python - 井字游戏的错误输出
问题描述
我无法更正此代码的输出:
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)
我得到这个输出。据我了解,xchecker_tool 函数定义不正确,这就是为什么即使没有排列“x”或“o”,它也会每两圈打印一次“恭喜”消息。我还希望帮助清理脚本,因为它非常冗长,但由于经常出现错误,我目前的水平无法做到这一点。
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!
解决方案
这里:
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
将被执行,其返回值将绑定到变量。您定义了三个变量 - 因此print
将执行三次,将文本输出到终端。
像这样的代码:
if board[1]==mark and board[2]==mark and board[3]==mark:
horizontal
仅仅“提及”变量horizontal
,它并没有真正执行任何事情。你也可以这样写:
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]
这些都不会执行任何操作。
您应该在if
语句中打印:
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
推荐阅读
- sql - 从 hive 中的 xml 值创建新列
- java - java程序以错误的顺序打印对象
- javascript - javascript if else 速记和字符串
- jquery - 如何在 tooltipster 类中使用数据数组?
- angular - 为什么异步观察者会进入循环?
- python - 使用 cx_Oracle 和 Lambda 层,获取 AWS Lambda 错误:尝试检索文本时出现错误 ora-01804 时出错
- vue.js - process.env 的 URL 前缀
- django - 在 Django 视图中,如何像在模板中一样格式化日期?
- google-sheets - 如何根据范围从单元格中提取数值
- android - 如何从 sqlite Android 获取 id