4th topic of Loops


#1

Why does the loop have to be..

while choice != "y" and while choice != "n":

instead of

while choice != "y" or while choice != "n": ?

Why AND over OR. If you only need to meet one condition and for AND you need the two conditions to be true, to be true. For OR you just need one condition to be true. Why does AND work?


#2

and is dependent upon all operands in the relationship being True where as or needs only one operand to be True.

a = True
b = False
a and b    # False
a or b     # True

#3

exactly. if the raw_input fails to get y and n then it should return false. while y or n should return true...


#4

@mm31jr,

1

The instruction:

  • Fill in the loop condition so the user will be prompted for a choice over and over while choice does not equal 'y' and choice does not equal 'n'.

    choice != "y" and while choice != "n"

2

The Purpose

Looking at the AND truth-Table

  AND  | true  | false
       |  !n   |   =n
 ------|-------|-------
 true  | true  | false
  !y   | Loop  | exit
 ------|-------|-------
 false | false | false
  =y   | exit  | exit

= = = = = = = = =

  OR   | true  | false
       |  !n   |   =n
 ------|-------|-------
 true  | true  | true
  !y   | Loop  | Loop
 ------|-------|-------
 false | true  | false
  =y   | Loop  | exit
 ------|-------|-------