TypeError:无法解压缩不可iterable的非类型对象python函数

2024-04-20 11:06:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个终极Tictatcoe游戏,与一个AI一起与一个用户进行游戏。为了确定AI将在何处放置O,我对以下代码进行了编码:

def AIsemidecentboy(allowed, grid):
print("hi")
x = allowed[0] # x = the location from where man is allowed to be
y = allowed[1] #variable y =the location from where man is allowed to be on the y plane
if allowed[0] == -1 and allowed[1] == -1:
    for x in range(allowed[0]+1, (allowed[0] + 10)): # when x is in the range of the x plane allowed positions
        for y in range(allowed[1]+1, (allowed[1] + 10)): # when y is in the range of the y plane allowed positions
            if grid[x][y] == 0: # if the position in the grid is not filled
                print(x,y)
                return(x, y) #return the first possible filling positions
else: # if the location where man is allowed to be isnt simply the whole thing and therefore is one of the minicubes
    for x in range(allowed[0], (allowed[0] + 3)): #when x is in the range of the 3 different x positions it can be in
        for y in range(allowed[1], (allowed[1] + 3)):# when y is in the range of the 3 different y positions it can be in
            if grid[x][y] == 0:
                print("MANLIKE")
                print(x,y)# if the position on the grid is available
                if grid[x][y+1] == 1:
                    print("hellman")
                    print(x,y)
                    return(x, y)
                elif grid[x][y-1] == 1:  
                    print(x,y)
                    print("hellman")                       # For all of these simply observe the notebook
                    return(x, y)
                elif grid[x+1][y] == 1:
                    print(x,y)
                    print("hellman")
                    return(x,y)
                elif grid[x+1][y+1] == 1:
                    print(x,y)
                    print("hellman")
                    return(x,y)
                elif grid[x-1][y+1] == 1:
                    print(x,y)
                    print("hellman")
                    return(x,y)
                elif grid[x+1][y-1] == 1:
                    print(x,y)
                    print("hellman")
                    return(x,y)
                elif grid[x-1][y-1] == 1:
                    print(x,y)
                    print("hellman")
                    return(x,y) 
            else: 
                print("filip")
                for x in range(allowed[0], (allowed[0] + 3)): #when x is in the range of the 3 different x positions it can be in
                    for y in range(allowed[1], (allowed[1] + 3)):# when y is in the range of the 3 different y positions it can be in
                        if grid[x][y] == 0:
                            print(x,y)
                            return(x,y)

此函数用于返回x和y的值,然后将其作为算法所做的决策输入。这些范围之所以存在,是因为人们可以根据游戏执行的随机化功能,将其x/o放置在9乘9字段或特定的3乘3框中

函数在第一次调用时工作,并转到最后一个else,在那里打印“filip”,但是在此之后,返回以下错误:

Traceback (most recent call last):
File "/Users/skanderlejmi/Desktop/ask.py", line 398, in <module>
xcord, ycord = AIsemidecentboy([allowedx, allowedy], grid)
TypeError: cannot unpack non-iterable NoneType object

我假定,出于某种原因,启动函数的代码在确定x和y值之前提取它们,或者函数返回的是为空的x和y值。不管是哪种方式,我都特别困惑,为什么在某个场合,函数最终会传递到另一个场合,但在另一个场合,它不会

我真的很感激任何人在这件事上的帮助,因为我还是一个初学者


Tags: oftheinforreturnifisrange