【问题标题】:Issues with For loops inside of Functions in Python 3.3Python 3.3 中函数内部的 For 循环问题
【发布时间】:2013-10-04 21:03:04
【问题描述】:

好的,所以我正在编写一个井字游戏,并且遇到了一个我似乎无法解决的非常糟糕的错误。我创建了一个功能,如果玩家即将获胜,计算机将阻止玩家,但是,如果成功阻止一次,即使条件满足也不会再触发。该函数的代码是:

def block():
for t in range(0, 9, 3):
        if slot[t] == user_team and slot[t+1] == user_team and (slot[t+2] \
           != user_team) and (slot[t+2] != computer_team):
            slot[int(t+2)] = computer_team
            return
        elif slot[t+1] == user_team and slot[t+2] == user_team and (slot[t] \
             != user_team) and (slot[t] != computer_team):
            slot[int(t)] = computer_team
            return
        elif slot[t] == user_team and slot[t+2] == user_team and (slot[t+1] \
             != user_team) and (slot[t+1] != computer_team):
            slot[int(t+1)] = computer_team
            return

for t in range(3):
        if slot[t] == user_team and slot[t+3] == user_team and (slot[t + 6] \
           != user_team) and (slot[t+6] != computer_team):
            slot[int(t+6)] = computer_team
            return
        elif slot[t+3] == user_team and slot[t+6] == user_team and (slot[t] \
             != user_team) and (slot[t] != computer_team):
            slot[int(t)] = computer_team
            return
        elif slot[t] == user_team and slot[t+6] == user_team and (slot[t+3] \
             != user_team) and (slot[t+3] != computer_team):
            slot[int(t+3)] = computer_team

此外,user_team 和 computer_team 会返回该玩家是 X 还是 O,并且 slot[int()] = computer_team 用于在棋盘上放置移动。 下面是调用函数的地方(以防我在这里搞砸了。):

else:
    draw_board()
    '''win()'''
    block()
    cmove()
    turn = "user"
    if end_game() == True:
        computer_win += 1
        draw_board()
        print ("The computer has won! But... We already knew that would happen. (:")
        playing = False
    elif end_game() == "Tie":
        tie_win += 1
        draw_board()
        print ("The game is a tie. You're going to have to try harder " \
               + "\n" + "if you wish to beat the computer!" + "\n")
        playing = False
    else:
        pass

如果你们中的任何人能告诉我哪里出错了,那我就开心了。 c:

Board(打印是缩进的,它只是不想在这里。)

def draw_board():
'''Opted to use lists so that the numbers can be replaced with either
    X or O later on and so that testing whether the game is over is simpler'''
print (" " + str(slot[0]) + " | " + str(slot[1]) + " | " + str(slot[2]))
print ("-----------")
print (" " + str(slot[3]) + " | " + str(slot[4]) + " | " + str(slot[5]))
print ("-----------")
print (" " + str(slot[6]) + " | " + str(slot[7]) + " | " + str(slot[8]))
print ("\n")

新错误:

这是我第 4 步后的棋盘

X | ○ | X

O | 4 | 5

X | 7 | X

第4步后电脑的棋盘(两步,替换一个x)

X | ○ | X

O | 4 |哦

O | 7 | X

【问题讨论】:

  • 这里的错误是什么?请务必包含完整的回溯。
  • 请注意,将else: 套件与if: 一起使用从来不是必需else: pass 语句可以安全删除。
  • 没有真正的跟踪簿,因为它不会产生一个实际的“错误”。发生的情况是,在计算机阻塞一次之后,程序似乎只是跳过了 block() 函数。 (如果条件满足,则不会做出阻止玩家获胜的动作)
  • 我没有看到任何可能或可能无法在代码中的任何位置调用 block() 的条件。
  • 我也没有,这就是我感到困惑的原因。 :O 你想看看完整的源代码吗?

标签: python function for-loop


【解决方案1】:

我相信您的问题在于您的 block 函数的逻辑。

这是你的董事会:

0 1 2
3 4 5
6 7 8

浏览第一对嵌套的for 循环,让我们看看你的代码做了什么:

for t in range(0,9,3):
    for y in range(1, 9, 3):

这将为您提供t, y 的以下对:(0,1), (0,4), (0,7), (3,1), (3,4), (3,7) , (6,1), (6,4) 和 (6,7)。马上,我不认为这是你的本意。据我所知,您正在尝试检查玩家是否连续有两个标记。

这个问题很容易解决 - 您不需要两个 for 循环。相反,只需使用tt+1t+2

接下来,考虑一行:

0 1 2

要检查三个条件,玩家在 0 和 1、0 和 2 或 1 和 2 处有标记。您只需检查其中两个条件 - 0 和 1,以及 1 和 2。

此外,if 语句并没有像您认为的那样做:

if ... and slot[y+1] != user_team and computer_team:

这等价于:

if ... and (slot[y+1] != user_team) and computer_team:

我假设computer_team'x''o',在这种情况下,python 将在if 语句中使用它与True 相同。你想要的是这样的:

if ... and (slot[y+1] != user_team) and (slot[y+1] != computer_team):

这也可能是您的代码只能工作一次的原因 - 下次它评估之前找到的同一行或列时,if 语句将再次评估为 True,它会设置相同又是空间,在你看来,它好像什么也没做。

您检查列的代码也有同样的问题。希望我指出的问题足以让您弄清楚如何修复您的代码。祝你好运!

【讨论】:

  • 谢谢!这几处变化确实创造了奇迹。 c: 但是我遇到了另一个错误。 ;O 如果最后两个动作在两个底角(仅发生在这两个角落),那么计算机将进行一次移动,以及替换 X 的第二次移动。我已经用新块更新了原始帖子() 函数和新错误,如果您不介意再读一遍?
  • 别介意那个错误!我忘了更新我的 win() 函数,因为它的结构与我的旧版本非常相似。您的建议解决了我遇到的所有问题。 (:感谢您的时间和帮助!
  • @GavinFeriancek 如果 Rob 解决了你的问题,看起来他很努力地做到了,你也应该接受他的回答......
  • @beroe 好点!我今天才开始使用这个网站,直到 20 分钟前才知道绿色检查。 :P 我现在已经接受了,当然我非常感谢他所做的所有工作。 c:
  • 优秀。作为奖励,这是一个很小的“代价”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-26
  • 2020-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多