将函数返回的值分配给Python中的变量

Hun*_*hod 3 python

我最近开始用Python编码,遇到一个问题,将函数返回的值赋给变量.

class Combolock:
    def _init_(self,num1,num2,num3):
        self.x = [num1,num2,num3]
    def next(self, state):
        print "Enter combination"
        combo = raw_input(">")
        if combo == self.x[state]:
            print "Correct"
            return 1
        else:
            print "Wrong"
            return 0
    def lock(self):
        currentState = 0
        while currentState < 2:
            temp = next(currentState)
            if temp == 1:
                currentState = currentState + 1
            else:
                currentState = 99
                print "ALARM"
Run Code Online (Sandbox Code Playgroud)

当我调用锁定函数时,我在行处出错

temp = next(currentState)
Run Code Online (Sandbox Code Playgroud)

说int对象不是迭代器.

And*_*mbu 8

您应该使用self.next(currentState),如您所希望next的类范围中的方法.

该功能next是全球性的next(obj),如果只能obj是一个迭代器.
您可能希望查看python文档中的yield语句.