0

I am trying to produce a list of odd numbers using a generator (just to get a better insight into generators). I wrote the following code but, it doesn't stop running! While I expect the code stops when the condition i>n meets. Any help is appreciated.

import sys
def odd(n):
    i=0    
    while True: 
        if i%2==0:
            continue
        yield i
        i+=1
        if i>n:
            return
# Here we build a generator 
g = odd(10)
while True:
    try:
        print(next(g),end=' ')
    except StopIteration:
        sys.exit()
5
  • 7
    When i is even, you never increment it. It stays even forever. Commented Jan 27, 2020 at 15:59
  • You aren't raising a stopiteration, you're just returning. Commented Jan 27, 2020 at 15:59
  • 3
    @Neil That's essentially what return does in a generator function. Commented Jan 27, 2020 at 16:00
  • 3
    @chepner still weird to use while loop and try/except on StopIteration instead of for Commented Jan 27, 2020 at 16:02
  • 1
    @CorentinLimier That doesn't really have anything to do with the question, though. Commented Jan 27, 2020 at 16:04

2 Answers 2

2

When i is even, you don't increment it, so it stays even for every subsequent iteration of the loop and never gets larger than n.

You want to increment i whether or not it is even.

def odd(n):
    i=0    
    while True: 
        if i%2 != 0:  # yield i only if it is odd
            yield i
        i+=1  # Increment i in either case
        if i>n:
            return
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, @chepner. You are right, I had completely forgotten to increase i when it is even. Actually I was practicing some commands in python and I wanted to use continue to see how it works.
0

In my opinion, you have two style issues in your code that make it hard to see the problem:

  1. The use of continue. A simple if statement would make it easier to see which code might not execute and which code will definitely execute. continue is mainly useful when you have nested if statements making things complicated.

  2. You don't utilize the while condition. This assumes that the while loop will have to execute at least once. When writing a loop, you should generally consider what happens if the loop needs to execute 0 times. What if someone passes an argument of -1? What if you change the initial value of i to 1 to save an iteration?


def odd(n):
    i = 0
    while i <= n:
        if i % 2:
            yield i
        i += 1
    # Automatically return and throw StopIteration.

3 Comments

I don't know how my code isn't formatted. It's indented by 4 spaces.
I added a hard rule to separate the code from the numbered list. I don't know why a blank line doesn't do that.
Thank you luther. Your notes were really helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.