Float对象不可iterable[Python]

Float对象不可iterable[Python],python,Python,嘿,我是python编程新手,我在函数中遇到了一个问题,而函数中没有浮点数。 这基本上是一个值迭代算法来解决蛇梯游戏,我有一个错误:在我的算法的Q函数中,float对象是不可迭代的。 我知道这个错误很小,所以我希望你能很容易地帮助我。 非常感谢 class Snakes_Ladders(object): def __init__(self, N): self.N=N def Begin(self): return 1 def is

嘿,我是python编程新手,我在函数中遇到了一个问题,而函数中没有浮点数。 这基本上是一个值迭代算法来解决蛇梯游戏,我有一个错误:在我的算法的Q函数中,float对象是不可迭代的。 我知道这个错误很小,所以我希望你能很容易地帮助我。 非常感谢

class Snakes_Ladders(object):


    def __init__(self, N):
        self.N=N

    def Begin(self):
        return 1


    def isEnd(self, state):

        return state == self.N

    def discount(self):
        return 1.

    def states(self):
        return range(1, self.N+1)

    def actions(self, state):
        result=[]
        if state<=self.N or state+1<=self.N:
            result.append('Security')
        if state<=self.N or state+1<=self.N or state+2<=self.N:
            result.append('Normal')
        return result

    def TransiMat(self, state, action):
        result = []
    #Returns a list with (NewState,Proba, Reward)
        if action=='Security'and state!=3:
            result.append((state, 0.5, -1))
            result.append((state+1, 0.5, -1))
        if action=='Security' and state==3:
            result.append((state, 0.5, -1))
            result.append((state+1, 0.25, -1))
            result.append((state+8, 0.25, -1))
        if action=='Normal' and state!=3:
            result.append((state, 0.33, -1))
            result.append((state+1, 0.33, -1))
            result.append((state+2, 0.33, -1))
        #fast lane
        if action=='Normal' and state==3:
            result.append((state,0.33,-1))
            result.append((state+1,0.165,-1))
            result.append((state+8,0.165,-1))
          ##Traps
        if action=='Normal' and state==5:
            result.append((0, 0.33, -1))
            result.append((state+1, 0.33, -1))
            result.append((state+2, 0.33, -1))
        if action=='Normal' and state==7:
            result.append((state-3, 0.33, -1))
            result.append((state+1, 0.33, -1))
            result.append((state+2, 0.33, -1))
        if action=='Normal' and state==9:
            result.append((state, 0.33, -1))
            result.append((state+1, 0.33, -1))
            result.append((state+2, 0.33, -1))
        return result


def ValueIteration(game):

    V={} #Initialization
    for state in game.states():
        V[state] = 0.

    def Q(state,action):
        i = 0.
        for result in game.TransiMat(state, action):
                newState=result[0]
                prob=result[1]
                reward=result[2]
                print(type(newState))
        **return sum(prob*(reward + game.discount()*V[newState]))** #The error is called here : Float 
                                                                      object is not iterable

    while True:
        #Compute new values given the old values

        newV = {}
        for state in game.states():
            if game.isEnd(state):
                newV[state]=0.
            else:
                newV[state] = max(Q(state,action) for action in game.actions(state))
  #Convergence
        if max(abs(V[state]- newV[state]) for state in game.states())<1e-10:
            break
        V=newV

  #Policy

    pi={}
    for state in game.states():
        if game.isEnd[state]:
            pi[state]= 'none'
        else:
            pi[state] = max((Q(state,action), action) for action in game.actions(state))[1]

game=Snakes_Ladders(N=15)
#print(game.actions(3))
print(game.TransiMat(1,'Security'))

ValueIteration(game)

class\u梯形图(对象):
定义初始化(self,N):
self.N=N
def Begin(自我):
返回1
def isEnd(自身、状态):
返回状态==self.N
def折扣(自助):
返回1。
def状态(自身):
返回范围(1,自N+1)
def操作(自身、状态):
结果=[]
如果状态
(iterable,/,start=0)

将iterable的起始项和项从左到右求和,并返回总数。iterable的项通常是数字,并且起始值不允许是字符串

sum
中添加一个默认列表应该可以做到这一点-

def Q(state,action):
    i = 0.
    for result in game.TransiMat(state, action):
        newState=result[0]
        prob=result[1]
        reward=result[2]
        print(type(newState))
    return sum([prob*(reward + game.discount()*V[newState])])
或者,如果只需要一个值,那么使用
sum
没有任何意义,只需返回该值即可

def Q(state,action):
    ...
    return  prob*(reward + game.discount()*V[newState])

您试图在那里求和什么?查看如何创建一个
求和
需要一个序列,但您传递的是一个数字。
折扣
显式返回一个浮点(
1.
),因此“我没有浮点”不是真的。但是,
sum
到底应该加什么呢?我的列表只包含整数,所以调用sum时应该不会有问题。但是它确实调用了V[newState]上的一个类型错误,表示浮点不能被迭代
sum
函数需要一个iterable作为输入,很高兴知道,我会做出必要的改变,看看它是否奏效。谢谢,这没用
sum([…])
列表中只有一个元素是不可操作的,如果您想要返回该元素,也可以返回该元素。假设OP确实需要一些东西的总和,但这并不能解决问题,问题需要更多细节才能得到答案。
def Q(state,action):
    ...
    return  prob*(reward + game.discount()*V[newState])