gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:29:08 25 4
gpt4 key购买 nike

我最近开始使用 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"

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

temp = next(currentState)

说 int 对象不是迭代器。

最佳答案

您应该使用 self.next(currentState),因为您希望在类作用域中使用 next 方法。

next 函数是全局函数,next(obj) 仅在 objiterator 时有效.
你可能想看看 yield statement在 python 文档中。

关于python - 将函数返回的值分配给Python中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7338781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com