Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning the value returned by a function to a variable in Python

Tags:

python

I began coding in Python recently and encountered a problem assigning the value returned by a function to a variable.

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"

When I call the lock function, I get an error at the line

temp = next(currentState)

saying that an int object is not an iterator.

like image 366
Hunterhod Avatar asked Feb 13 '26 09:02

Hunterhod


1 Answers

You should use self.next(currentState), as you want the next method in the class scope.

The function next is global and next(obj) works only if obj is an iterator.
You might want to have a look at the yield statement in the python documentation.

like image 163
Andrea Ambu Avatar answered Feb 15 '26 00:02

Andrea Ambu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!