在一开始时,值得注意的是我是Python的初学者。
我的if
陈述有问题。我正在尝试写一条线,该线将检查玩家是否选择X或O而不是其他角色。所以我用那条线来做到这一点:
if player_x_or_o == 'X' or 'O':
setplayermove(row_choice, col_choice)
else:
print('Invalid value, try again.')
但是,如果有人使用,例如“ S”,则程序将首先执行if
而不是else
。
我不明白为什么,有什么想法吗?
# functions
def game_board(X_or_O=0, row=0, column=0, show_gb=False):
print(' 1 2 3')
if not show_gb:
game[row][column] = X_or_O
for index, listof0 in enumerate(game):
index += 1
print(index,'.',listof0)
def setplayermove(row, col):
game[row_choice][col_choice] = player_x_or_o
# Game board
game = [[0,0,0],
[0,0,0],
[0,0,0]]
game_board(show_gb=False)
# User input
while True:
player_x_or_o = str(input('Do you want put "X" or "O"? \n'))
row_choice = int(input('In which row do you want set your {}? \n'.format(player_x_or_o)))
col_choice = int(input('In which column do you want set your {}? \n'.format(player_x_or_o)))
row_choice -= 1
col_choice -= 1
if player_x_or_o == 'X' or 'O':
setplayermove(row_choice, col_choice)
else:
print('Invalid value, try again.')
game_board(show_gb=True)
答案 0 :(得分:2)
我认为这行是错误的:
if player_x_or_o == 'X' or 'O':
您可能要写的是:
if player_x_or_o == 'X' or player_x_or_o == 'O':
答案 1 :(得分:0)
第二个条件始终为真,请尝试如下更改条件:
if player_x_or_o == 'X' or player_x_or_o == 'O'