简体   繁体   English

为什么 break 语句在这里不起作用?

[英]Why does the break statement not work here?

I just wanna preface this by saying that I'm a beginner and I'm sorry if this is a stupid question/obvious mistake.我只是想先说我是初学者,如果这是一个愚蠢的问题/明显的错误,我很抱歉。

I'm making a human vs computer tic tac toe game, and the break statement is not breaking the innermost loop.我正在制作人机对战井字游戏,而 break 语句并没有打破最内层的循环。 The problem arises in the very last loop where even if the human wins, the if conditon below it is not executed.问题出现在最后一个循环中,即使人类获胜,它下面的 if 条件也不会执行。 It goes on to play the computer's move and THEN declare that the computer has won.它继续下计算机的动作,然后宣布计算机赢了。

please do inform me if I should change or add something to my question.如果我应该更改或添加问题,请通知我。

Thank You谢谢你


boardkeys={9:'     ',8:'     ',7:'     ',6:'     ',5:'     ',4:'     ',3:'     ',2:'     ',1:'     '}

def wincon():
    if boardkeys[1]==boardkeys[2]==boardkeys[3]!='     ':
        return True
    elif  boardkeys[4]==boardkeys[5]==boardkeys[6]!='     ':
        return True
    elif  boardkeys[7]==boardkeys[8]==boardkeys[9]!='     ':
        return True
    elif  boardkeys[1]==boardkeys[4]==boardkeys[7]!='     ':
        return True
    elif  boardkeys[2]==boardkeys[5]==boardkeys[8]!='     ':
        return True
    elif  boardkeys[3]==boardkeys[6]==boardkeys[9]!='     ':
        return True
    elif  boardkeys[1]==boardkeys[5]==boardkeys[9]!='     ':
        return True
    elif  boardkeys[3]==boardkeys[5]==boardkeys[7]!='     ':
        return True

hsym="  O  "
csym="  X  "

def ai():
    z=random.randrange(1,10)
    while boardkeys[z]!="     ":
        z=random.randrange(1,10)
    boardkeys[z]=csym

def move():
    no=int(input("enter your move human"))
    if boardkeys[no]=="     ":
        boardkeys[no]=hsym
    else:
        print("invalid move")
        move()
                              
for i in range(10):
    count=0
    move()
    count+=1
    printboard()
    if wincon==True:                     ????
        print("Game Over you won")       ????        
        break                            ????
    elif count==9:
        print("TIED")
        break
    else:
        print()
        print("computer's turn")
        ai()
        printboard()
        if wincon()==True:
            print("Game Over computer won")
            break
        elif count==9:
            print("TIED")
            break

    

Instead of if wincon==True: it should be if wincon()==True: , you're calling a function, not reading a boolean.而不是if wincon==True:它应该是if wincon()==True: ,你正在调用 function,而不是读取 boolean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM