1

I just learned while loops in Python, and would like some help in why the following two codes have different outputs. Since the while loop stops at iteration at when the squares element is not 'orange', I thought the output for both should be ['orange', 'orange']. Could you please explain the difference between two?

# code1
squares = ['orange', 'orange', 'red']
new_squares = []

i=0
square=squares[0]

while(square=='orange'):
    new_squares.append(square)
    square=squares[i]
    i = i + 1
print(new_squares)


# code2
squares = ['orange', 'orange', 'red']
new_squares = []

i = 0
while(squares[i] == 'orange'):
    new_squares.append(squares[i])
    i = i + 1
print (new_squares)

-- EDIT --

If I were to change the order of the first code to as suggested in one of the answers, the output for is now ['orange', 'orange', 'red']. Why does 'red' get appended to the output if it fails the condition?

And so if I wanted to change the first code to have the output of ['orange', 'orange'], what would I have to do?

# code1
squares = ['orange', 'orange', 'red']
new_squares = []

i=0
square=squares[0]

while(square=='orange'):
    square=squares[i]
    new_squares.append(square)
    i = i + 1
print(new_squares)
1
  • 6
    the first one goes square = squares[0] twice. Commented May 23, 2020 at 17:41

4 Answers 4

1

The first while has an additional "square=squares[i]". If you change it like this, you will get the same results:

# code1
squares = ['orange', 'orange', 'red']
new_squares = []

i=0
square=squares[0]

while(square=='orange'):
    square=squares[i]
    new_squares.append(square)
    i = i + 1
print(new_squares)


# code2
squares = ['orange', 'orange', 'red']
new_squares = []

i = 0
while(squares[i] == 'orange'):
    new_squares.append(squares[i])
    i = i + 1
print (new_squares)
Sign up to request clarification or add additional context in comments.

1 Comment

The this new code now outputs ['orange', 'orange', 'red']. Why does it append 'red' in the output if it fails the condition?
1

SEE COMMENTS TO UNDERSTAND HOW IT WORKS FOR ALL PASSES:

    # code1
squares = ['orange', 'orange', 'red']     #ist pass                2nd pass                    3rd pass                   4th pass 
new_squares = []

i=0                                       # i=0
square=squares[0]                       # square='orange'

while(square=='orange'):                # true                      true                        true                    false
    new_squares.append(square)          # orange0 appended          orange0 appended again      orange1 appended
    square=squares[i]                   # as i=0, square='orange'   i=1,square='orange1'      i=2, so square='red'
    i = i + 1                           # now i=1                    i=2                       i=3
print(new_squares)                         


# code2
squares = ['orange', 'orange', 'red']    
new_squares = []

i = 0                                   #i=0                           
while(squares[i] == 'orange'):          # true                        true                       false
    new_squares.append(squares[i])      # orange0 appended          orange1 appended as i=1
    i = i + 1                           # i=1                       i=2
print (new_squares)

Comments

0

Think about how the code runs:

squares = ['orange', 'orange', 'red']
new_squares = []

i=0
square=squares[0]


while(square=='orange'):
    new_squares.append(square)
    square=squares[i]
    i = i + 1
print(new_squares)

On iteration one:

squares = ['orange', 'orange', 'red']
new_squares = []

i=0
square=squares[0] #square = 'orange'


while(square=='orange'):
    new_squares.append(square) # new_squares = ['orange']
    square=squares[i] # i = 0; squares[0] = 'orange'
    i = i + 1 # i = 1
print(new_squares)

Second iteration:

squares = ['orange', 'orange', 'red']
new_squares = []

i=0



while(square=='orange'):
    # square = 'orange' from previous 
    new_squares.append(square) # new_squares = ['orange', 'orange']
    square=squares[i] # i = 1 from previous; squares[1] = 'orange'
    i = i + 1 # i = 2
print(new_squares)

Final iteration:

squares = ['orange', 'orange', 'red']
new_squares = []

i=0



while(square=='orange'):
    # square = 'orange' from previous 
    new_squares.append(square) # new_squares = ['orange', 'orange', 'orange']
    square=squares[i] # i = 2 from previous; squares[2] = 'red'
    i = i + 1 # i = 3
print(new_squares)

Comments

0

Take look on first code. And exactly let's pay attention on the first iteration in loop.

# First of all, you set `square` to zero element in array.

i=0
square=squares[0]   # <---- here it is. 

while(square=='orange'):   # <--- then you check is zero element still "orange", and it is
    new_squares.append(square)
    square=squares[i]      # i still equal to 0. here you again set `square` to zero element in array. I believe you want to set it on next element. But your i still = 0.
    i = i + 1              # and only here i set to 1, so you should cnange the order of this command
print(new_squares)

Here is the fixed version:

i=0
square=squares[0]

while(square=='orange'):
    new_squares.append(square)
    i = i + 1     # here firstly you increase i, and now it 1
    square=squares[i]  # here you get the next element

print(new_squares)

Comments

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.