1

I just learned about break and return in Python.

In a toy code that I wrote to get familiar with the two statements, I got stuck in a loop, but I don't know why. Here is my code:

def break_return():
    while True:
        for i in range(5):
            if i < 2:
                print(i)
            if i == 3:
                break
        else:
            print('i = ', i)
            return 343
break_return()

I'm new to programming, any suggestions will be appreciated.

3
  • For problems like this, it's very helpful to run your program in an interactive visualizer or the built-in debugger, to see the flow of control visually instead of just guessing at what it might be that could cause this behavior. Commented Sep 10, 2018 at 1:03
  • @abarnert this is very helpful! Thank you! Commented Sep 10, 2018 at 1:10
  • I wish more people knew about PythonTutor. (Well, if every Python novice knew about it, it might bankrupt Philip Guo or whoever's hosting it for him, because it's so useful that everyone would be using it all the time while learning…) Commented Sep 10, 2018 at 1:26

2 Answers 2

4

With the for-else construct you only enter the else block if the for loop does not break, which your for loop always does because i inevitably becomes 3 with your range generator. Your infinite while loop is therefore never able to reach the return statement, which is only in the said else block.

Sign up to request clarification or add additional context in comments.

Comments

-2

nvm I'm super wrong here

First of all, when you define a function in Python, any code that belongs in the function should be in the same indentation block. With this in mind, your code would look like this:

def break_return():
    while True: 
        for i in range(5):
            if i < 2:
                print(i)
            if i == 3:
                break
        else:
            print('i = ', i)
            return 343
break_return()

The next problem I see is that your else statement isn't correctly formatted with an if statement. If you mean for it to go on the 2nd if statement, your code would look like this:

def break_return():
    while True: 
        for i in range(5):
            if i < 2:
                print(i)
            if i == 3:
                break
            else:
                print('i = ', i)
                return 343
break_return()

This is only formatting. But in this example, the code would only run once because it immediately returns and exits the function.

I think this may be a better example of using both break and return:

def break_return(value):
    for i in range(5):
        print(i)
        if i == 3:
            break #This exits the for loop
        if i == 4:
            print("This won't print!")
            #Won't print because the loop "breaks" before i ever becomes 4
    return value * 2 #Returns the input value x 2

print(break_return(30)) #Display the return value of break_return()

This demonstrates how break exits a for loop and how return can return a value from the function.

The output of the code above is:

0  #Value of i
1  #Value of i
2  #Value of i
3  #Value of i
60 #The value returned by the function

Glad to hear you're learning Python! It's a lot of fun, and super useful.

1 Comment

The OP is asking about a for…else statement.

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.