Python 当我匹配了所有的配对时,我怎样才能结束我的记忆游戏?

Python 当我匹配了所有的配对时,我怎样才能结束我的记忆游戏?,python,python-3.x,while-loop,counter,Python,Python 3.x,While Loop,Counter,我已经完成了几乎所有的代码。我的问题是,我在定义clicks()下实现的计数器不工作。我有16个项目,所以当我的等色比较运行8次时,我希望函数停止。我截取了很多不相关的代码,主要关注的是clicks()的第一个if语句 要使while循环工作,您需要将计数器的初始化(count=0)置于while循环之外 此外,假设您希望在计数达到8时退出循环,则需要允许它检查计数(如果计数>=8)。使用elif意味着如果if-条件为True,则不会检查此项 def clicks(): game_run

我已经完成了几乎所有的代码。我的问题是,我在定义clicks()下实现的计数器不工作。我有16个项目,所以当我的等色比较运行8次时,我希望函数停止。我截取了很多不相关的代码,主要关注的是clicks()的第一个if语句


要使while循环工作,您需要将计数器的初始化(
count=0
)置于while循环之外

此外,假设您希望在计数达到8时退出循环,则需要允许它检查计数(
如果计数>=8
)。使用
elif
意味着如果
if
-条件为
True
,则不会检查此项

def clicks():
    game_running = True
    count = 0 # Initialize count before while loop
    while game_running:
        first_click = win.getMouse()
        x_cell1 = int(first_click.getX()//50)
        y_cell1 = int(first_click.getY()//50)
        (first_r, first_c) = click_loc(first_click)
        first_r.undraw()
        second_click = win.getMouse()
        x_cell2 = int(second_click.getX()//50)
        y_cell2 = int(second_click.getY()//50)
        (second_r, second_c) = click_loc(second_click)
        second_r.undraw()
        rgb1 = circles[y_cell1][x_cell1]
        rgb2 = circles[y_cell2][x_cell2]
        if rgb1[0] == rgb2[0] and rgb1[1] == rgb2[1] and rgb1[2] == rgb2[2]:
            count += 1
        if count >= 8: # use if instead of elif here 
            game_running = False
        else:
            first_r.draw(win)
            second_r.draw(win)
    win.close()

作为一个小建议,在您的情况下应该不重要,但我发现更可靠的是使用“等于或大于”
=
,而不是“等于”
=
,首先确保count=0不在while循环中,因为您在迭代时不断重置它。 其次,将
elif count==8
更改为
if count==8
,因为当您使用
elif

当语句中的计数实际达到8时,它可能会跳过
elif count==8

如果rgb1[0]==rgb2[0]和rgb1[1]==rgb2[1]和rgb1[2]==rgb2[2]:
计数+=1

因此,如果将其替换为
,则确保它将检查计数器状态

最终代码如下所示:

def clicks():
count = 0
game_running = True
while game_running:
    first_click = win.getMouse()
    x_cell1 = int(first_click.getX()//50)
    y_cell1 = int(first_click.getY()//50)
    (first_r, first_c) = click_loc(first_click)
    first_r.undraw()
    second_click = win.getMouse()
    x_cell2 = int(second_click.getX()//50)
    y_cell2 = int(second_click.getY()//50)
    (second_r, second_c) = click_loc(second_click)
    second_r.undraw()
    rgb1 = circles[y_cell1][x_cell1]
    rgb2 = circles[y_cell2][x_cell2]
    if rgb1[0] == rgb2[0] and rgb1[1] == rgb2[1] and rgb1[2] == rgb2[2]:
        count += 1
    if count == 8: 
        game_running = False
    else:
        first_r.draw(win)
        second_r.draw(win)
win.close()

我怀疑这与您在while循环中设置
count=0
计数永远不会达到8有关?请尝试将“elif count==8:”更改为“if count==8”,然后为什么要重置计数器?
def clicks():
count = 0
game_running = True
while game_running:
    first_click = win.getMouse()
    x_cell1 = int(first_click.getX()//50)
    y_cell1 = int(first_click.getY()//50)
    (first_r, first_c) = click_loc(first_click)
    first_r.undraw()
    second_click = win.getMouse()
    x_cell2 = int(second_click.getX()//50)
    y_cell2 = int(second_click.getY()//50)
    (second_r, second_c) = click_loc(second_click)
    second_r.undraw()
    rgb1 = circles[y_cell1][x_cell1]
    rgb2 = circles[y_cell2][x_cell2]
    if rgb1[0] == rgb2[0] and rgb1[1] == rgb2[1] and rgb1[2] == rgb2[2]:
        count += 1
    if count == 8: 
        game_running = False
    else:
        first_r.draw(win)
        second_r.draw(win)
win.close()