Orololuwa
Orololuwa

Reputation: 13

I'm having issues using generators with the input function in python 3

def prime():
    n = 1000
    if n<2:
        return 0
    yield 2
    x = 3
    while x <= n:
        for i in range(3,x,2):
            if x%i==0:
                x+=2
                break
        else:
            yield x
            x+=2

#if i try to call each yielded value with an input function i don't get anything!

def next_prime():
    generator = prime()
    y = input('Find next prime? yes/no or y/n: ')
    if y[0].lower == 'y':
        print(generator.next())

next_prime()

#but if i call the function without using an input i get my values back


generator = prime()
def next_prime():
    print(next(generator))

next_prime()

How do I make the first next_prime function work with the input function. If I try to call each yielded value with an input function, I don't get anything but if I call the function without using an input, I get my values back. Is it that generators don't work with the input function?

Upvotes: 1

Views: 54

Answers (1)

DaVinci
DaVinci

Reputation: 879

The mistake you have done is you have forgot the round brackets of the lower keyword

def next_prime():
    generator = prime()
    y = input('Find next prime? yes/no or y/n: ')
    #forgot the round brackets 
    if y[0].lower() == 'y':
        print(next(generator))

next_prime()

Upvotes: 2

Related Questions