What is the output of this code

What is the output for the following lines of code? i=0 while i<5: if (i -2) == 1: break print i i +=1

  • 0 1 2
  • 0 1 2 3 4
  • 0 1 2 4
  • 0 1 2 3 4 5

Why don’t you just run it and find out?

1 Like

Welcome, Inas.

As Ariel said, the best thing would be for you to run it.

For future posts, when you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

This is especially important for Python code, because with the way you currently have it formatted, we do not know what the indentation should be. So, we cannot even begin to help with the question.

1 Like
# run this code in your editor
i = 0
while i < 5:
    done = (i-2) == 1
    if done:
        break
    print(i)    
    i += 1  

@Sky020
Thanks for your advice :ok_hand:

The result was

0
1
2

Unnecessary semicolon pylint(unnecessary-semicolon) [5,1]