How to print an element from a list each time a particular method is called?

0

I'm trying to make a math expression game in python, but I need my MandaExp method to return an expression every time it's called. In this method I have a list called explist, where several expressions are organized according to the selection of difficulty of the user. Searching for some things but the way it is it only returns a list with the indices, that is, a list that goes from 0 to 430. Here is the part of the code:

class MotorDoJogoSP(object):
import operator
def MandaExp(self, dif):
    Dificultador.Sort = Dicio[dif]
    expr = Dificultador().Sort()
    explist = [operator.attrgetter('expressao')(x) for x in expr]
    resplist = [operator.attrgetter('resposta')(x) for x in expr]
    for e in range(len(explist)):
        yield e

If you do print list(MotorDoJogoSP().MandaExp(dif)) it returns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ..., 430]

The expected output was the elements of explist, each time it was called the method would output an element from the list, Example: 36 + 0, 30/0, ...

    
asked by anonymous 31.07.2016 / 23:39

1 answer

1

So I added the index to a new list, the correction was:

class MotorDoJogoSP(object):
import operator
def MandaExp(self, dif):
    Dificultador.Sort = Dicio[dif]
    expr = Dificultador().Sort()
    explist = [operator.attrgetter('expressao')(x) for x in expr]
    resplist = [operator.attrgetter('resposta')(x) for x in expr]
    for e in explist:
        yield e

And I'm using next() to print one after the other, how many times it's called.

    
01.08.2016 / 00:56