While Loops


#1

In the below statement why we cant use the "or" instead of "and"

choice = raw_input('Enjoying the course? (y/n)')

while choice != 'y' and choice != 'n': # Fill in the condition (before the colon)
choice = raw_input("Sorry, I didn't catch that. Enter again: ")


#2

Hi @techiyappa ,

With the or operator, if either operand is True, the result of the or operation is True.

The while loop will iterate as long as the condition is True.

If we use or instead of and, we have this while loop header ...

while choice != 'y' or choice != 'n':

Let's evaluate the condition for various user inputs ...

  • For an input of 'x', both operands are True, so the entire condition is True.

  • For an input of 'y', the second operand is True, so the entire condition is True.

  • For an input of 'n', the first operand is True, so the entire condition is True.

We have an infinite loop.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.