next() call not iterating

var = 10
Constant = 10
def Gen():
    i = 1
    for x in range(var):
        yield i
        i +=1   
o = Gen()

c = next(o) * Constant

for i in range(var):
    print(c)

What I’ve tried and their errors:

c = {next(o)} * Constant
        #unsupported operand type(s) for *: 'set' and 'int'
c = int({next(o)}) * Constant
        #int() argument must be a string, a bytes-like object or a real number, not 'set'`

Expected output:

10
20
30
40 
...

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

When you call next() you are asking for just one value. Since you want to iterate on the values returned by your generator you may write a loop, or a set comprehension, for instance:

c = {z * Constant for z in o}

The syntax you used just got a single value, put it in a set, and tried to multiply the set by Constant.

Side note: why a set and not a list?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x