Function 函数中的循环

Function 函数中的循环,function,loops,python-3.x,Function,Loops,Python 3.x,为什么计算机没有变成x?在它通过循环并找到列表中而不是拾取中的东西之后,它不应该将x的值分配给计算机吗 def ComputerMove(gameboard): for x in list: if x not in picked and x in list: computer = x list = ("b2", "a1", "c3", "a3", "c1", "a2", "b3", "b1", "c2") gameboard = {0:

为什么计算机没有变成x?在它通过循环并找到列表中而不是拾取中的东西之后,它不应该将x的值分配给计算机吗

def ComputerMove(gameboard):
    for x in list:
        if x not in picked and x in list:
            computer = x 



list =  ("b2", "a1", "c3", "a3", "c1", "a2", "b3", "b1", "c2")

gameboard = {0:' ', 1:' ', 2:' ',
         3:' ', 4:' ', 5:' ',
         6:' ', 7:' ', 8:' ',
         }
picked = ("b2", "a1", "c3", "a3")
ComputerMove(gameboard) 
首先,list是一种类型/已重新保存,所以不能将其用作变量名。其次,您拥有的是一个元组,而不是一个列表。因此,我对这些代码进行了转换,这段代码似乎有效:

def ComputerMove(gameboard):
    for x in l:
        if x not in picked:
            computer = x
            return computer # not sure what do you want to do with this value?



l =  ["b2", "a1", "c3", "a3", "c1", "a2", "b3", "b1", "c2"]

gameboard = {0:' ', 1:' ', 2:' ',
         3:' ', 4:' ', 5:' ',
         6:' ', 7:' ', 8:' ',
         }
picked = ["b2", "a1", "c3", "a3"]
ComputerMove(gameboard) 
>>c1

ComputerMove应该返回/执行什么操作?这只是调用函数,以便它可以实际运行。您可以使用保留关键字作为变量名,但不建议这样做。@SteinarLima IMO不建议这样做。覆盖类型和关键字是Python中最大的禁忌,这就是为什么我经常告诉新手这是不允许的。仅仅因为口译员会允许你,并不意味着它是好的。例如,在Py2.x中,你可以做True,False=False,True,任何头脑清醒的人都会这样做吗=