加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 12:18:53 所属栏目:Python 来源:网络整理
导读:我最近开始用 Python编码,遇到一个问题,将函数返回的值赋给变量. class Combolock: def _init_(self,num1,num2,num3): self.x = [num1,num3] def next(self,state): print "Enter combination" combo = raw_input("") if combo == self.x[state]: print "Cor
我最近开始用 Python编码,遇到一个问题,将函数返回的值赋给变量.

class Combolock:
    def _init_(self,num1,num2,num3):
        self.x = [num1,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"

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

temp = next(currentState)

说int对象不是迭代器.

解决方法

你应该使用self.next(currentState),因为你想要类范围中的下一个方法.

函数next是global,next(obj)仅当obj是iterator时才有效.
您可能想要查看python文档中的yield statement.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读