【问题标题】:How to do use While loop with many conditions to meet?#如何使用满足多个条件的While循环?#
【发布时间】:2017-07-15 02:28:07
【问题描述】:

我设法让 2 个玩家轮流玩,但在第 2 个玩家输入她的答案后它就停止了。我想做While循环,但是我真的不知道该怎么做。我的想法是,我希望两个玩家继续玩直到 0 或 X 的 3 个位置被填满,但是将它放入代码中我真的需要帮助。

以下是我的条件,但我希望它继续询问玩家,直到满足这些条件

if list1[0,1,2] == 'x':
print ('Congrats! x won!')
elif (list1 [0] , list2 [0] , list3[0]) == 'x':
print ('Congrats! x won!')
elif (list1 [0] , list2 [1], list3[2]) == 'x':
print ('Congrats! x won!')
elif (list2 [0] , list2 [1], list2[2])== 'x':
print ('Congrats! x won!')
elif (list3 [0] , list3 [0], list3[0]) == 'x':
print ('Congrats! x won!')
elif (list1 [1] , list2 [1], list3[1]) == 'x':
print ('Congrats! x won!')
elif (list1 [2] , list2 [2], list3[2]) == 'x':
print ('Congrats! x won!')
elif (list1 [0] , list2 [1], list3[0]) == 'x':
print ('Congrats! x won!')
elif (list1 [1] , list2 [1], list3[1]) == 'x':
print ('Congrats! x won!')
elif (list1 [2] , list2 [2], list3[2]) == 'x':
print ('Congrats! x won!')
elif (list1 [0] , list2 [0], list3[0]) == 'x':
print ('Congrats! x won!')
elif (list1[0,1,2] == 'o':)
print ('Congrats! o won!')
elif (list1 [0] , list2 [0] ,list3[0]) == 'o':
print ('Congrats! o won!')
elif (list1 [0] , list2 [1], list3[2]) == 'o':
print ('Congrats! o won!')
elif (list2 [0] , list2 [1], list2[2]) == 'o':
print ('Congrats! o won!')
elif (list3 [0] , list3 [0], list3[0]) == 'o':
print ('Congrats! o won!')
elif (list1 [1] , list2 [1], list3[1]) == 'o':
print ('Congrats! o won!')
elif (list1 [2] , list2 [2], list3[2]) == 'o':
print ('Congrats! o won!')
elif (list1 [0] , list2 [1], list3[0]) == 'o':
print ('Congrats! o won!')
elif (list1 [1] , list2 [1], list3[1]) == 'o':
print ('Congrats! o won!')
elif (list1 [2] , list2 [2], list3[2]) == 'o':
print ('Congrats! o won!')
elif (list1 [0] , list2 [0], list3[0]) == 'o':
print ('Congrats! o won!') 
else

【问题讨论】:

  • 等等,这是一个 3 列井字游戏,你正在检查是否有人赢了?
  • 您的任何条件都不会为真,因为您正在比较不同的对象。我建议你看看the python tutorial
  • 是的,我使用了 3 个列表。 list1 , list2 和 list3
  • 此代码让您收到来自冗余部门的警告!
  • 下次,请记得对问题进行简要概述。人们不必猜测您正在尝试实现井字游戏。

标签: python list if-statement while-loop


【解决方案1】:

将你的看板作为一个包含 3 个列表的列表(每个列表包含 3 个元素)会更容易。

使用allany,可以避免很多不必要的重复:

board = [['x', None, 'o'],
         ['o', 'x', None],
         ['o', None, 'x']
         ]


def three_in_row(board, player):
    return any(all(board[j][i] == player for i in range(3)) for j in range(3))


def three_in_column(board, player):
    return any(all(board[i][j] == player for i in range(3)) for j in range(3))


def three_in_diagonal(board, player):
    return all(board[i][i] == player for i in range(3)) or\
        all(board[i][2 - i] == player for i in range(3))


print three_in_row(board, 'x')
# False
print three_in_column(board, 'x')
# False
print three_in_diagonal(board, 'x')
# True

【讨论】:

    【解决方案2】:

    试试这个:

    def check_line(line):
        if line[0]==line[1] and line[1]==line[2]:
            return line[0]
        return ' '
    
    def check_win(board):
        #check horizontals
        for row in board:
            res=check_line(row)
            if res != ' ':
                return res
    
        #check verticals
        x=0
        for col in board[0]:
            res=check_line([board[y][x] for y in range(0, 3)])
            x+=1
            if res != ' ':
                return res
    
        #check diagonals
        for diagonal in ( [board[0][0], board[1][1], board[2][2]], [board[0][2], board[1][1], board[2][0]]):
            res = check_line(diagonal)
            if res != ' ':
                return res
    
        return ' '
    
    def show_winner(list1, list2, list3):
        winner=check_win([list1, list2, list3])
        if winner!=" ":
            print("Congrats! {0} won!".format(winner))
    
    list1=['o', 'x', 'x']
    list2=['o', 'x', ' ']
    list3=['x', 'o', ' ']
    show_winner(list1, list2, list3)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-18
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      • 2023-01-12
      • 2018-12-22
      相关资源
      最近更新 更多